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

Could someone please advise on how I could revise this code to get each line number counted represented by $countline into an array where line number 1 is in element 1 of the array and line 2 is in element 2 of the array?
use strict; my $countline = 0; my $mytest; open (MYFILE,"<testing.txt")||die "Could not open input file\n"; while($mytest = <MYFILE>) { my @datas= split (/\s+/, $mytest); if ($datas[0] eq "Hello") { $countline++; } my @test1 = $countline; print $test1[0]; }

Replies are listed 'Best First'.
Re: help with an array
by blazar (Canon) on Jul 29, 2005 at 15:26 UTC
    use strict;
    use warnings; # as well!
    my $countline = 0; my $mytest; open (MYFILE,"<testing.txt")||die "Could not open input file\n";
    (A matter of personal preference but
    open my $fh, '<', "testing.txt" or die "Could not open input file: $!\ +n";
    may be better. Pay attention to all the details...)
    while($mytest = <MYFILE>) {
    You may want
    while( defined($mytest = <MYFILE>) ) {
    to be more precise. Which is a good reason IMHO to use Perl's idiomatic
    while(<MYFILE>) {
    my @datas= split (/\s+/, $mytest); if ($datas[0] eq "Hello")
    Incidentally, no need to create the array:
    if ( (split /\s+/, $mytest)[0] eq "Hello" )
    works just fine. Also, splitting on /\s+/ is actually different from splitting on ' ' (see perldoc -f split) but the latter is in most cases what one really wants, and in fact it is also the default. So, had you also used the topicalizer as hinted above, this may have been just as simple as
    if ( (split)[0] eq "Hello" )
    and in that case I would have used it as a statement modifier:
    $countline++ if +(split)[0] eq "Hello";
    my @test1 = $countline; print $test1[0];
    Huh?!?

    all in all if I understand correctly what that you want to achieve, this may have been just as simple as

    while (<>) { push @wanted, $_ if +(split)[0] eq "Hello" }
    (but you may also want to chomp.)
Re: help with an array
by ikegami (Patriarch) on Jul 29, 2005 at 15:07 UTC

    Use push(@array, $mytest); where you currently count:

    use strict; use warnings; my $mytest; my @array; open(MYFILE, '<', 'testing.txt') || die "Could not open input file\n"; while ($mytest = <MYFILE>) { my @datas = split(/\s+/, $mytest); push(@array, $mytest) if $datas[0] eq "Hello"; } # my $countline = @array; print("The matching lines:\n"); foreach my $line (@array) { print($line); }
    or
    use strict; use warnings; open(MYFILE, '<', 'testing.txt') || die "Could not open input file\n"; my @array = grep { my @datas = split(/\s+/, $_); $datas[0] eq "Hello" } <MYFILE>; # my $countline = @array; print("The matching lines:\n"); foreach my $line (@array) { print($line); }
      push was my first thought, but that would start the array off with element 0, not 1 as requested. The original post can have multiple interpretations though, so it's hard to say exactly what the desired result is without further clarification.
        so add an extra push at the top.
Re: help with an array
by kwaping (Priest) on Jul 29, 2005 at 15:09 UTC
    I might be misunderstanding the question, but if not, I think the solution is as easy as this:
    use strict; my $countline = 0; my $mytest; my @array = (); #<- added line open (MYFILE,"<testing.txt")||die "Could not open input file\n"; while($mytest = <MYFILE>) { my @datas= split (/\s+/, $mytest); if ($datas[0] eq "Hello") { $countline++; $array[$countline] = $countline; #<- added line } my @test1 = $countline; print $test1[0]; }
Re: help with an array
by anonymized user 468275 (Curate) on Jul 29, 2005 at 15:24 UTC
    Looks like a job for grep.
    use strict; open (MYFILE,"<testing.txt")||die "Could not open input file\n"; my @datas = grep /^Hello\s+/, <MYFILE>; my $countline = $#datas + 1; # or just treat $#datas as linecount-1

    One world, one people