Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Help in reading log file

by Anonymous Monk
on Mar 14, 2008 at 06:29 UTC ( #674155=perlquestion: print w/replies, xml ) Need Help??

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

Hi Monks,

I have to read lines from a log file which begins with // with some white spaces before and put them in a array so below is the code what i could write and this works. But i want to know if i can remove the spaces before pushing into array instead of using s/^\s+//g; in foreach loop while printing and also i want to ignore the lines with -// which is getting included.

open (FH,"$file") || die ("could not openfile"); my @arr; while (<FH>) { chomp; push @arr , $_ unless $_ !~ /\s+\/\// ; } foreach (@arr){ s/^\s+//g; s/^-\/\/.+//g; print $_."\n"; } Thanks in advance.

Replies are listed 'Best First'.
Re: Help in reading log file
by hipowls (Curate) on Mar 14, 2008 at 08:51 UTC

    There are a few things I'd do differently, first the open I'd write as

    open my $FH, '<', $file or die "Can't open $file: $!\n";
    • The lexical file handle $FH is limited is scope to just the block of code it appears in so won't clobber file handles opened else where. Probably not a concern for a short script but it still a good habit.
    • The three argument form of open where the open mode is specified separately is safer especially when $file comes from an untrusted source.
    • I use low precedence or so I can get rid of the brackets
    • I put $! in the error message so I know why the open failed.

    The loop I'd rewite as this

    use Data::Dumper; my @arr; while (<DATA>) { chomp; push @arr, $1 if m{^ +\s+(//.*)}; } print Dumper \@arr; __DATA__ // humpty dumpty // sat on a wall -// eating her curds and whey // humpty dumpty // had a great fall
    which produces
    $VAR1 = [ '// humpty dumpty', '// sat on a wall', '// humpty dumpty', '// had a great fall' ];
    Just a couple of points
    • I use m{ } to avoid the leaning toothpicks.
    • I use capturing parentheses to grab the bit of string I want, if there is a match it is in $1 and it is pushed onto the array

Re: Help in reading log file
by Punitha (Priest) on Mar 14, 2008 at 07:05 UTC

    Hi

    On modifying your code, you can try like this,

    open (FH,"$file") || die ("could not openfile"); my @arr; while (<FH>) { chomp; push @arr , $1 if $_ =~ /\s+(\/\/.+)/; }

    Punitha

Re: Help in reading log file
by jwkrahn (Monsignor) on Mar 14, 2008 at 07:08 UTC

    It appears that lines with  -// are included because you don't anchor the pattern  /\s+\/\// to the beginning of the line. So it looks like you probably want something like:

    open FH, '<', $file or die "could not open '$file' $!"; my @arr; while ( <FH> ) { chomp; push @arr, $_ if s!^\s+(?=//)!! ; } foreach ( @arr ){ print "$_\n"; }
Re: Help in reading log file
by poolpi (Hermit) on Mar 14, 2008 at 07:11 UTC
    #!/usr/bin/perl use strict; use warnings; use Data::Dumper; my @arr; while (<DATA>){ chomp; push @arr, $_ if s/ \s+ ( [\/]{2} .+ ) /$1/xms; } print Dumper \@arr; __DATA__ //opiopiopi -//popoiop uytyu --- //llmlm //fghfghf -//sdsdfsdfs
    Output : $VAR1 = [ '//opiopiopi', '//llmlm', '//fghfghf' ];

    hth,

    PooLpi

    'Ebry haffa hoe hab im tik a bush'. Jamaican proverb

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://674155]
Approved by Thilosophy
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (4)
As of 2020-07-04 06:01 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?