in reply to printing column info without headers

use strict; use warnings; my_search('data.txt'); sub my_search { my $file = shift; open my $in_fh, '<', $file or die "Unable to open $file: $!\n"; while (<$in_fh>) { next if (/^Number1/); my @Numbers = split; print "@Numbers\n"; } close $in_fh; }

Some comments:

Replies are listed 'Best First'.
Re^2: printing column info without headers
by annie06 (Acolyte) on Jul 09, 2008 at 15:46 UTC
    thanks, what happens if the if in the 'next' line isn't true. Does the script then move down to the following line of code in the script??
      The code I showed is extremely simple because your input file example is extremely simple. The code I presented will read in a file and will ignore all lines which begin with the string "Number1" because of the next statement. For all other lines which do not begin with "Number1", the numbers will be stored in an array. The script will generate warnings if there are blank lines (because of the print).

      Your actual file probably has a more complicated structure than your example file, but I can not predict its structure without more information from you.

      reason I'm asking is because it seems to me we would then break out of the while loop. I don't want to do that, I'd like to still be able to continue on and look for other strings in the file..etc..
        next will not break out of the while loop; last would break out of the loop.

        I personally believe you shouldn't be (mis)guided by what could possibly seem to you, but rather resort to the actual docs, which tell you what that is. Incidentally, YMMV but I personally find "next" very intuitive as a term to suggest that it won't break out of the loop: just as much as "last" is about the fact that it will.

        (Apologies for replying so late.)

        --
        If you can't understand the incipit, then please check the IPB Campaign.
      thanks, what happens if the if in the 'next' line isn't true.

      (If that's a question, then it should be closed with a question mark, incidentally.)

      I personally believe that as with any other C<if>, if the condition isn't true, then the statement, i.e. next is not executed. Then, as with (nearly) any other statement, (that it has a statement modifier doesn't change this) control is passed to the following one. Pretty ordinary stuff, as you can see.

      (Apologies for replying so late.)

      --
      If you can't understand the incipit, then please check the IPB Campaign.