Global symbol "$fnames" requires explicit package
This specific error is because you have an array named @fnames but you later try to dereference an undeclared arrayref named $fnames.
You've also got an issue with the part of your syntax that tries to access the element within the array:
That probably would not do what you think even if you didn't have the first problem. You can't access an element of an array, whether directly or via reference, by a key name.@$fnames->{filename}
If, as seems possible from the code you posted, you are receiving into your sub listdirs() two arguments, each a reference to an array, you can get what I think you want with:
Or you could get the list of filenames with something like:my $res_files = list_dirs($dir,$datanames); sub list_dirs { my ( $locations, $fnames ) = @_; ... foreach my $href ( @{ $fnames } ) { my $filename = $href{'filename'}; # do something with filename ... } }
But, this raises the question of why you have an array of anonymous hashes containing just one key-value pair?my $res_files = list_dirs($dir,$datanames); sub list_dirs { my ( $locations, $fnames ) = @_; my @filenames = map { $_->{'filename'} } @{ $fnames }; ... }
And in general, in Perl, there's usually a tool to do something common like what you want. You might like to use Path::Iterator::Rule:
use Path::Iterator::Rule; ... my $res_files = list_dirs($dir,$datanames); sub list_dirs { my ( $locations, $fnames ) = @_; # assumes each is a simple arrayref my $finder = Path::Iterator::Rule->new; $finder->name( @{ $fnames } ); my $get_next_match = $finder->iter( @{ $locations } ); my @found_filenames; while ( defined ( my $match = $get_next_match->() ) ) { push @found_filenames, $match; } return \@found_filenames; }
See also Yes, even you can use CPAN.
Hope this helps!
In reply to Re: Passing file names to search directories
by 1nickt
in thread Passing file names to search directories
by Anonymous Monk
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |