in reply to Pulling lines from file, noob style

Obviously, since you only showed 1 line of your input, this is just a guess, but perhaps you really want to only print if the regex matches, something like this:
while( <SMRT> ){ chomp (); if (/Power_On_Hours(.*)/i) { $hours = $1 ; @time = split(' ',$hours); print "Power on hours = $time[7]\n"; } }
Also, use strict and warnings, as they would probably have given you some useful debugging information in this case.

Replies are listed 'Best First'.
Re^2: Pulling lines from file, noob style
by jperkins (Initiate) on Feb 17, 2010 at 17:33 UTC
    Yes! That did it! It looks like I closed that expression early than? Is that why it picked up more than what I asked?

      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.

        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.

        Cheers,

        JohnGG