Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I know this has been asked a million times from different people but I'm confused as to why this isn't working.
print header, start_html('log parsing'); open(FILE, "log.txt") or die "Cannot open file: $!"; while(<FILE>) { my $message = <FILE>; if ($message =~ m/(^\Alberta226: )([a-zA-Z, 0-9])/) { print "$1: $2<br>"; } } close (FILE) or die "Cannot close file: $!";
I am trying to open a file log.txt which is a chat file. It is formated like:
user1: hi there user2: hi buddy Alberta226: okay
Why does my regex not pull back anything by Alberta226 when I see it in the actual log file?

Thanks

Replies are listed 'Best First'.
Re: Parsing a file
by broquaint (Abbot) on Jan 22, 2004 at 17:58 UTC
    You're reading the file twice
    while(<FILE>) { ## assign $_ to readline *FILE my $message = <FILE>; ## assign my $message to readline *FILE
    Which is one problem, and another is you're using the regex anchor \A which matches the beginning of a string. So you're matching berta226: to the beginning of the string, which will also make the regex fail. If you change your code to something like this you should get the desired output (updated - dropped extraneous semi-colon on second line. update 2: also fixed a further 2 mistakes as noted below .oO(note to self - don't post in a hurry shortly before leaving ... ))
    while(my $message = <FILE>) { print "$1: $2<br/>" if $message =~ /^(Alberta226: )([a-zA-Z, 0-9]+)/; # I added the + }
    See. perlsyn and perlre for more info.
    HTH

    _________
    broquaint

      I know I'm not the smartest person here or even really all that great with regexes, but shouldn't the + be inside the ()'s like ([a-zA-Z, 0-9]+)? Otherwise it'll only trap the first character in the rexex.


      "Age is nothing more than an inaccurate number bestowed upon us at birth as just another means for others to judge and classify us"

      sulfericacid
      I tried your snippet but it keeps erroring out on if /^.., do you have any idea why?
        I think the line before the if wasn't supposed to have a semi-colon (;) at the end.
      This is so weird. I rid of the colon and it doesn't crash, but it doesn't print or find anything. An exact copy of my log file is
      Paltalk: This is a G rated voice group intended for a General Audience + including minors. Offensive language is not permitted. To speak, h +old the ctrl key down. ladymindy: gu ladymindy: jo ladymindy: hi pixelatedcyberdust: hi Alberta226: ((((( LM )))))) ladymindy: ((((((much)))))) ladymindy: i can t read ladymindy: (((((((alkberta)()))))) eartistp: hey pixels awake too eartistp: can't type either today lol pixelatedcyberdust: yep, very awake Alberta226: much is here ???
        If you're using this code:
        while(my $message = <FILE>) { print "$1: $2<br/>" if /^(Alberta226: )([a-zA-Z, 0-9])+/; # I added the + }

        you'll need to fix one little thing. The lines from FILE are being assigned to $message but the regex is matching against $_

        So you could do this as
        while(<FILE>) { print "$1: $2<br/>" if /^(Alberta226: )([a-zA-Z, 0-9])+/; }

        or
        while(my $message = <FILE>) { print "$1: $2<br/>" if $message =~/^(Alberta226: )([a-zA-Z, 0-9])+/; }
Re: Parsing a file
by allolex (Curate) on Jan 22, 2004 at 18:07 UTC

    You're not quite there with filehandles, yet. First you are telling Perl to get each line (actually record) in a file, one at a time, using while. Then you're making a reference to that same filehandle again while the line you want is already in the special variable $_.

    Try this (untested) code:

    print header, start_html('log parsing'); open(FILE, "<", 'log.txt') or die "Cannot open file: $!"; while (<FILE>) { # $_ is your message, but you could also # say "while my $message ..." to store # each line in $message chomp; # gets rid of newlines, etc. if ( m/(^Alberta226:)\s+(.*)/ ) { # you just need # the one anchor print "$1: $2<br>\n"; # add a newline for HTML output legibility } } close (FILE) or die "Cannot close file: $!";

    --
    Allolex