in reply to column counter and printf question

Hello fasoli,

I want to ignore the first 12 lines...

If you know in advance that the number of lines to ignore is exactly 12, you can use the range operator in scalar context like this:

next if 1 .. 12;

(This works because it’s short for next if ($. == 1 .. $. == 12);) But:

In line 12, which is one line above where I want to start data copying, there is a unique pattern (@TYPE xy). I'd prefer to match this line and then tell the script to start copying from the next (13) line onwards.

In this case, change the range as follows:

while (<$input>) { next if 1 .. /\@TYPE xy/; printf ...; }

See perlop#Range-Operators.

Hope that helps,

Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

Replies are listed 'Best First'.
Re^2: column counter and printf question
by fasoli (Beadle) on Oct 28, 2015 at 14:06 UTC

    Thanks so much for that! I had also found this suggestion somewhere and tried it, but there was no context given so I found myself trying it the wrong way and it was then discarded as "not working".

    I have updated my question with a new attempt I made at counting the columns, do you think I'm getting a bit closer...? Also, should I put it in a separate post too instead of just an update to the original post?

    Thank you again :)

      toolic and scorpio17 have given you two ways to print out the columns. Here’s a variation on the theme:

      while (my $line = <$input>) { next if 1 .. $line =~ /\@TYPE xy/; my @columns = split /\s+/, $line; printf $output "%8.3f", $columns[2]; printf $output " %10.3f", $columns[$_] for 3 .. $#columns; print $output "\n"; }

      This makes use of the fact that for any array @array, the variable $#array contains the value of the highest array index. (Note that this is one less than the number of elements in the array, because array indices start at 0.)

      Hope that helps,

      Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,