in reply to Search List
Additionally, why use ls?
opendir(my $dh, '.') or die("Unable to list directory: $!\n"); my @searchlist = readdir($dh);
or
opendir(my $dh, '.') or die("Unable to list directory: $!\n"); while (defined(my $file = readdir($dh))) { ... }
Combined with sachmet's solution:
opendir(my $dh, '.') or die("Unable to list directory: $!\n"); my @patterns = map { qr/\Q$_\E/ } grep { /^\.\.?$/ } readdir($dh); open(my $fh_in, 'test.txt') or die("Unable to open input file test.txt: $!\n"); while (my $line = <$fh_in>) { chomp $line; foreach my $pattern (@patterns) { print("Match found\n") if $line =~ $pattern; } }
Combined with holli's solution:
opendir(my $dh, '.') or die("Unable to list directory: $!\n"); my %searchlist = map { $_ => 1 } grep { /^\.\.?$/ } readdir($dh); open(my $fh_in, 'test.txt') or die("Unable to open input file test.txt: $!\n"); while (my $line = <$fh_in>) { chomp $line; print("Match found\n") if $searchlist{$line}; }
Update: Fixed copy & paste error map { qr/\Q$pattern\E/ } => map { qr/\Q$_\E/ }.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Search List
by Roy Johnson (Monsignor) on Dec 20, 2005 at 17:26 UTC | |
by ikegami (Patriarch) on Dec 20, 2005 at 17:51 UTC |