in reply to Re: Problem with regex
in thread Problem with regex
Of course, that prints every matching line in the file, if you only want the first (evidenced by "last"), you could just use:my @FILES = grep { -f } readdir (TEMP); foreach my $file (@FILES){ open (F, $file) or warn "Couldn't open $file: $!" and next; my @matches = grep { /$str/ } <F>; close (F); next unless scalar @matches > 0; print join("\n", @matches); }
And do you really want to use "die" in a loop? What if you can't open one file, but you can open the rest? Isn't "warn and next" more appropriate?print $matches[0], "\n";
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^3: Problem with regex
by johngg (Canon) on Sep 16, 2008 at 14:49 UTC | |
by mpeever (Friar) on Sep 16, 2008 at 17:11 UTC |