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

This node falls below the community's threshold of quality. You may see it by logging in.

Replies are listed 'Best First'.
Re: How to count the number of columns in a line
by Skeeve (Parson) on Aug 31, 2006 at 12:10 UTC
    Your code (that, what you wanted) should be as easy as:
    my @match_A = split /\t/, $line; push @list_of_headers, @match_A[4..$#match_A];
    @list_of_headers= @match_A[4..$#match_A];
    No need for a loop...

    Update: Thanks to Hofmator for correcting me! ++

    s$$([},&%#}/&/]+}%&{})*;#$&&s&&$^X.($'^"%]=\&(|?*{%
    +.+=%;.#_}\&"^"-+%*).}%:##%}={~=~:.")&e&&s""`$''`"e
      Your code (that, what you wanted) should be as easy as:
      my @match_A = split /\t/, $line; @list_of_headers= @match_A[4..$#match_A];
      No need for a loop...
      ... provided @list_of_headers was empty beforehand! Otherwise use
      push @list_of_headers, @match_A[4..$#match_A];

      -- Hofmator

      Code written by Hofmator and posted on PerlMonks is public domain. It is provided as is with no warranties, express or implied, of any kind. Posted code may not have been tested. Use of posted code is at your own risk.

Re: How to count the number of columns in a line
by Velaki (Chaplain) on Aug 31, 2006 at 11:30 UTC

    It looks like you're trying to scan columns 4 though the end of the line for a particular pattern for each line.

    Perhaps this sorta golf-like snippet will help.

    #!/usr/bin/perl use strict; use warnings; my $pattern = qr/[45fg]/; # Look for "4,5,f,g" while(<DATA>) { chomp, print "$_\n" for grep { /$pattern/ } ((split /\t/)[3 .. s/\t(?!\n)/\t/g ]); } __DATA__ 1 2 3 4 5 6 7 8 9 108 9 a b c d e f g h i j

    Pax vobiscum,
    -v.

    "Perl. There is no substitute."
Re: How to count the number of columns in a line
by shmem (Chancellor) on Aug 31, 2006 at 09:46 UTC
    sniff... homework?

    Please see How do I post a question effectively?. Your code looks good so far.

    What should be looked for in the for loop?

    --shmem

    _($_=" "x(1<<5)."?\n".q·/)Oo.  G°\        /
                                  /\_¯/(q    /
    ----------------------------  \__(m.====·.(_("always off the crowd"))."·
    ");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}
    A reply falls below the community's threshold of quality. You may see it by logging in.
Re: How to count the number of columns in a line
by aquarium (Curate) on Aug 31, 2006 at 09:44 UTC
    for (4..@match_A) {
    the hardest line to type correctly is: stty erase ^H
      bit picky, but shouldn't that be
      for (4..$#match_A) {
      ?
Re: How to count the number of columns in a line
by davido (Cardinal) on Aug 31, 2006 at 16:55 UTC

    In scalar context, @array (or in your case, @match_A) evaluates to the number of elements in an array. That's not quite what you're looking for; you're looking for the highest index, which is @array - 1*. But Perl gives you a way to get the index of the last element without doing math; $#array (or in your case, $#match_A) will tell you the index of the ultimate element in the array.

    So, for( 4 .. $#match_A ) {....... will do the trick.

    *Note: @array - 1 will also be the index of the highest element, but that assumes you've left $[ alone, which you should. If you've messed with $[ you've committed an egregious foul.


    Dave