Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical

Re^3: Pulling lines from file, noob style

by xyzzy (Pilgrim)
on Feb 17, 2010 at 17:43 UTC ( #823775=note: print w/replies, xml ) Need Help??

in reply to Re^2: Pulling lines from file, noob style
in thread Pulling lines from file, noob style

It picked up exactly what you asked, but it printed even when it didn't pick anything up. as you use Perl, your mind will assimilate to a more perlish way of procedural thinking. for instance, the same snippet could be written as

while( <SMRT> ){ chomp; next unless /Power_On_Hours(.*)/i; $hours = $1; @time = split(' ',$hours); print "Power on hours = $time[7]\n"; }

although the overall effect is the same as using an if statement the intention is more explicit: you don't care about the line unless it matches the regex. as perl takes over you mind, you should notice a decrease in errors of this sort

$,=qq.\n.;print q.\/\/____\/.,q./\ \ / / \\.,q.    /_/__.,q..
Happy, sober, smart: pick two.

Replies are listed 'Best First'.
Re^4: Pulling lines from file, noob style
by johngg (Canon) on Feb 17, 2010 at 22:56 UTC

    Definitely more perlish, but why waste cpu clicks chomping lines that might be rejected? I'd move the chomp line to be immediately after the next unless ... line. I'd perhaps even question whether chomp was necessary here as the dot in the regex will not match the line terminator.

    J:\johngg>i:\cygwin\bin\hexdump.exe -C test.txt 00000000 61 61 61 61 61 31 32 33 0d 0a |aaaaa123. +.| 0000000a J:\johngg>perl -nle "next unless /^a+(.*)/; print qq{>$1<};" test.txt >123<

    I hope this is of interest.



Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://823775]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (5)
As of 2021-04-15 07:20 GMT
Find Nodes?
    Voting Booth?

    No recent polls found