Here the file is only read once. You could supply multiple strings you want to have present in the first array reference, and multiple strings in the second array reference where none of them should be present in the file. This simple subroutine only gives a boolean answer, but it could be extended to give more detailed answers what was matched and what not.use strict; use warnings; sub check_strings { my ($file, $r_stringsNeeded, $r_stringsToAvoid) = @_; open my $fh, '<', $file or die "Can't open '$file': $!\n"; my %foundNeeded = map { $_ => 0 } @{$r_stringsNeeded}; my %foundToAvoid = map { $_ => 0 } @{$r_stringsToAvoid}; while (<$fh>) { for my $string (@{$r_stringsNeeded}) { ++$foundNeeded{$string} if /\Q$string/; } for my $string (@{$r_stringsToAvoid}) { ++$foundToAvoid{$string} if /\Q$string/; } } my $foundAllNeeded = 0 == scalar grep { $_ == 0 } values %foun +dNeeded; my $foundNoneOfAvoided = 0 == scalar grep { $_ != 0 } values %foun +dToAvoid; return $foundAllNeeded && $foundNoneOfAvoided; } if (!check_strings('search.txt', ['first_neededString', 'second_needed +String'], ['firstStringToAvoid', 'secondStringToAvoid'])) { die "file did not contain all needed Strings or contained forbidde +n strings\n"; }
Hope that helps.
In reply to Re: Perl script to Parse a file for a string
by hexcoder
in thread Perl script to Parse a file for a string
by user786
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |