deadpickle has asked for the wisdom of the Perl Monks concerning the following question:

Here is what I want to do. I want to grep a list of hash references (as produced by the module Net::SFTP::Foreign->glob) to find a matching string. I could probably do this with loops but that seems inefficient.

What I am doing is globbing a remote directory for files: @remote_list = $sftp->glob("$remote_dir$radar/$product/$angle/$year*.netcdf.gz");

Then I loop through a list of local files produced by a python inline command:

@file_list = files($output, $radar, $product, $angle); foreach my $file (@file_list){

The next part is really what I am asking. I want to grep the list of hash references contained in @remote_list under the key ->{filename}. I have tried if (@match = grep (/$file/, @{$remote_list}->{filename})) { with what I have thought at no success. Was wondering if anyone had an idea on how to do this. Thanks.

Replies are listed 'Best First'.
Re: grep a list of hash references
by ikegami (Patriarch) on Mar 07, 2011 at 18:22 UTC

    I want to grep the list of hash references

    Indeed, but @{$remote_list}->{filename}} is not a list of hash references. That's in @remote_list according to what you said, so your grep will look like

    grep { ... } @remote_list

    So how you decide if a reference matches or not? By checking "its" filename against $file.

    grep { $_->{filename} eq $file } @remote_list

    But that's very inefficient. If you have 4 remote files and 5 local files, you are doing 4*5=20 comparisons when there's no reason to do more than 4.

    my %local_files = map { $_ => 1 } @local_files; for (@remote_files) { print "match\n" if $local_files{ $_->{filename} }; }