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
    One subtle difference between your use of readdir and the OP's use of ls is that the latter will not list dot-files (e.g., .profile) by default. Conveniently, glob('*') mimics that behavior and shortens the necessary code:
    my %searchlist = map {$_ => 1} glob('*');

    Caution: Contents may have been coded under pressure.

      Aye. The non-glob alternative would be to change
      grep { /^\.\.?$/ }
      to
      grep { /^\./ }