Re: newbie to perl



In article <3MyIe.70266$Ph4.2186225@xxxxxxxxxxxxxxxxxxxxxxx>, Billy
Sproket <sproketmoviesrule@xxxxxxxxxxxxxx> wrote:

> This may seem to be a bit complex for a newbie, but I'm looking for some
> ideas as to how to go about this.
>
> Here's my "pseudo code"
>
> Open a file
> read a line
> While not at EOF do
> {
>
> if the first token is the word LoadName
> write the line out
> go to the next line
> if the first token is the word Font
> change the token to be LoadFont
> endif
> write the line out
> else
> if the first token is the word PrintName
> write the line out
> go to the next line
> if the first token is the word Font
> change the token to be PrintFont
> endif
> write the line out
> else
> write the line out
> endif
> endif
> read a line
> }
>
> Thre's probably a really simple way to do this, but none of the examples
> I can find in any book (O'Reily etc) can't put the pieces together
> enough to tell me how to do what I need to do. Even if someone can
> point me to a resource where I can learn how to do this it would be
> better than me spinning my wheels for hours trying to figure it out on
> my own.

Something like this?:

#!/usr/local/bin/perl
#
use warnings;
use strict;

open(my $fh, '<', 'filename') or die("Can't open filename: $!");

while(<$fh>)
{
my @tokens = split;
if( $tokens[0] eq 'LoadName' ) {
print;
$_ = <$fh>;
s/^(\s*)Font/$1LoadFont/;
print;
}elsif( $tokens[0] eq 'PrintName' ) {
print;
$_ = <$fh>;
s/^(\s*)Font/$1PrintFont/;
print;
}else{
print;
}
}

This assumes that your 'tokens' are separated by whitespace.

FYI: this newsgroup is defunct. Try comp.lang.perl.misc in the future.


----== Posted via Newsfeeds.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! >100,000 Newsgroups
---= East/West-Coast Server Farms - Total Privacy via Encryption =---
.



Relevant Pages

  • Re: SQLEXEC()
    ... Check the return value for SQLEXEC() ... > I'm essentially a newbie when it comes to using the SQLCONNECT-related ... > ENDIF ...
    (microsoft.public.fox.programmer.exchange)
  • newbie to perl
    ... This may seem to be a bit complex for a newbie, but I'm looking for some ideas as to how to go about this. ... if the first token is the word Font ... Even if someone can point me to a resource where I can learn how to do this it would be better than me spinning my wheels for hours trying to figure it out on my own. ...
    (comp.lang.perl)
  • Re: A newbie question - line number inside the script
    ... I'm a newbie to Perl, ... use strict; ... use warnings; ...
    (perl.beginners)
  • Re: Renaming files
    ... > Can anyone help me in renaming about 1000 files I have. ... newbie answer: ... use strict; ... use warnings; ...
    (comp.lang.perl.misc)
  • Re: [git-pull -tip] Add -Werror to arch/x86/ files only and fix warnings
    ... => 'Turn all warnings into errors' ... Subject: x86: Adding -Werror to arch/x86/ files only for!CONFIG_ALLOW_X86_WARNINGS ... # Makefile for the ia32 kernel emulation subsystem. ...
    (Linux-Kernel)