jujiro_eb has asked for the wisdom of the Perl Monks concerning the following question:
Need your help again. I am using grep function to select certain file names in an array using file mask. When I use the a scalar variable as the regular expression pattern in the grep function, I get wrong result. A hard coded pattern returns the right result. Please see the code below.
use strict; my $folder="."; my $file_mask="\.txt"; my $file; opendir(DIR, "$folder"); my @files =grep(/$file_mask$/,readdir(DIR)); closedir(DIR); foreach $file (@files) { print "$file\n"; # Finds a file called 1txt (WHY) } opendir(DIR, "$folder"); @files =grep(/\.txt$/,readdir(DIR)); closedir(DIR); foreach $file (@files) { print "$file\n"; # Does not find the file called 1txt (As expected) }
I would greatly appreciate your input regarding this anomaly.
Ash
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Question: File name pattern match using Grep function
by BrowserUk (Patriarch) on May 05, 2009 at 02:07 UTC | |
|
Re: Question: File name pattern match using Grep function
by Anonymous Monk on May 05, 2009 at 02:02 UTC | |
|
Re: Question: File name pattern match using Grep function
by hexcoder (Curate) on May 05, 2009 at 14:27 UTC | |
|
Re: Question: File name pattern match using Grep function
by johngg (Canon) on May 05, 2009 at 21:38 UTC | |
|
Re: Question: File name pattern match using Grep function
by senthilkumar.k (Initiate) on May 06, 2009 at 16:53 UTC |