Yuhao
Yuhao

Reputation: 1610

Questions About Perl Filename Wildcard

I am using perl to address some text files. I want to use perl filename wild card to find all the useful files in a folder and address them one by one, but my there are spaces in the filename. Then I find the filename wildcard cannot address those filenames properly. Here is my code:

my $term = "Epley maneuver";
my @files = <rawdata/*$term*.csv>;
my $infiles;
foreach $infilename (@files) {
    if($infilename =~ m/(\d+)_.*\.csv/)
    {
        $infiles{$infilename} = $1;
        print $1."\n";
    }
}

The filename are like:

34_Epley maneuver_2012_4_6.csv
33_Epley maneuver_2012_1_3.csv
32_Epley maneuver_2011_10_12.csv
...

They are in a folder named "rawdata".

When I used this for terms that don't contain spaces, like "dizzy", it works well. But when the term contains space, it just stop working. I searched this on Google, but find little useful information.

What happens and how can I do this correctly? Any help will be good. Thanks a lot.

Upvotes: 3

Views: 5714

Answers (1)

Borodin
Borodin

Reputation: 126742

The glob operator works like the command-line processor. If you write <rawdata/*Epley maneuver*.csv> it will look for files that match rawdata/*Epley or maneuver*.csv

You must put your glob expression in double-quotes:

my @files = <"rawdata/*$term*.csv">

Upvotes: 5

Related Questions