in reply to Reading specific files

#!/usr/bin/perl -w opendir(DIR, "."); @files = grep(/^R/,readdir(DIR)); closedir(DIR); foreach $file (@files) { print "$file\n"; }
That works. Stolen from:
Here

Replies are listed 'Best First'.
Re^2: Reading specific files
by GrandFather (Saint) on Sep 21, 2009 at 23:43 UTC

    Don't steal. Stolen goods often have nasty baggage. In this case strict is missing, there is no error checking on the opendir, package variables rather than lexical variables are being used and the various open issues that may face the OP are not addressed. A better version of the code is:

    use strict; use warnings; opendir my $scanDir, $directory or die "Unable to open directory $dire +ctory: $!\n" my @files = grep /^R/, readdir $scanDir; closedir $scanDir; for my $filename (@files) { open my $inFile, '<', "$directory/$filename" or die "Can't open $d +irectory/$filename: $!"; ... close $inFile; }

    True laziness is hard work
      Fair enough, I just assumed (ya, I know) the OP would use strict as that is standard practice... As for the rest, it was just cut-n-paste but it worked. I was tempted to just say change it to /^R/ but whatever. Live and learn :)