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'
In reply to Re: For loop not executing all the values
by GrandFather
in thread For loop not executing all the values
by parthodas
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |