in reply to help with an array
use strict;
use warnings; # as well!
(A matter of personal preference butmy $countline = 0; my $mytest; open (MYFILE,"<testing.txt")||die "Could not open input file\n";
may be better. Pay attention to all the details...)open my $fh, '<', "testing.txt" or die "Could not open input file: $!\ +n";
You may wantwhile($mytest = <MYFILE>) {
to be more precise. Which is a good reason IMHO to use Perl's idiomaticwhile( defined($mytest = <MYFILE>) ) {
while(<MYFILE>) {
Incidentally, no need to create the array:my @datas= split (/\s+/, $mytest); if ($datas[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 asif ( (split /\s+/, $mytest)[0] eq "Hello" )
and in that case I would have used it as a statement modifier:if ( (split)[0] eq "Hello" )
$countline++ if +(split)[0] eq "Hello";
Huh?!?my @test1 = $countline; print $test1[0];
all in all if I understand correctly what that you want to achieve, this may have been just as simple as
(but you may also want to chomp.)while (<>) { push @wanted, $_ if +(split)[0] eq "Hello" }
|
|---|