in reply to For loop not executing all the values
Perl's for loop loops over a list. You have a single item in the list - the first line in the file. Instead you need a while loop which loops while a condition is true. Actually, you need just a little more work than that. You should loop until the file read returns undef. Consider:
#!/usr/bin/perl use strict; use warnings; my $fileName = "file_detail.txt"; open my $fOut, '>', $fileName or die "Can't create '$fileName': $!\n"; print $fOut <<CONTENT; 123 abc 12ab3c CONTENT close $fOut; open (my $fh, "<", $fileName); while (defined (my $value = <$fh>)) { chomp ($value); if ($value eq "abc") { print "matched 'abc'\n"; } elsif ($value eq "123") { print "matched '123'\n"; } }
Prints:
matched '123' matched 'abc'
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: For loop not executing all the values
by Athanasius (Archbishop) on Sep 01, 2015 at 08:37 UTC | |
by GrandFather (Saint) on Sep 02, 2015 at 00:35 UTC | |
by Anonymous Monk on Sep 02, 2015 at 00:41 UTC | |
|
Re^2: For loop not executing all the values
by parthodas (Acolyte) on Sep 01, 2015 at 07:25 UTC |