Reputation: 972
I have a directory where there are more than 10 files which start with pattern "my_Report". While i tried using glod for this job,it just picked only a single file . Is there any problem with the syntax below
$g_map{"Rep"} = glob ("data_1/reports/my_Report*");
Alternatively , i tried using grep to find all the files and stored it in a hash
$g_map{"Rep"} = [grep {!/\.xh$/} <data_1/reports/my_Report*>];
My Requirement is to find all the files with specific pattern from the directory and store it in a hash with key "Rep" How do i achieve the same with glob?
Thanks in Advance
Upvotes: 2
Views: 607
Reputation: 9296
glob
returns a list, but you're calling it in scalar context, which is why you're only getting a single result. Try this:
@{ $g_map{Rep} } = glob ("data_1/reports/my_Report*");
That'll turn $g_map{Rep}
hash key into an array reference, and all of the files will be stored in it.
You can access it like this:
for (@{ $g_map{Rep} }){
print "filename: $_\n";
}
Upvotes: 2
Reputation: 118605
Your first call is in scalar context. In scalar context, glob
returns (at most) a single result.
To retrieve all the matching files, use list context (like you do in your second call)
$g_map{"Rep"} = [ glob("data_1/reports/my_Report*") ]
or if you are expecting one result or just want the first result
($g_map{"Rep"}) = glob("data_1/reports/my_Report*");
Upvotes: 3