develooper Front page | perl.beginners | Postings from April 2010

matching values of one hash to another

Thread Next
Harry Putnam
April 28, 2010 20:51
matching values of one hash to another
Message ID:

I need to do some matching of filenames in two top level directories.
We expect to find a number of cases where the endnames ($_) are the
same in both hierarchies but the full name is different.

I've made hashes of the file names in two top level directories:

I've assembled the hashes using File::Find. We sort them first by
matching the base directory to the top directories passed in.

My first impulse wast to do it by matching the endnames (or value) in
one hash to any matching endname (value) in the other.  There are
several more actions that follow (not coded yet) but That's a lot of

I'm thinking there are better ways to do that.  So .. the script below
does it the hard way, with all that spinning.  Can anyone suggest
another way?

-------        ---------       ---=---       ---------      -------- 
use strict;
use warnings;

use File::Find;
use Cwd;

 my $r1 = shift;
 my $r2 = shift;
 my %r1h;
 my %r2h;

my $r1hkcn = 0;
my $r2hkcn = 0;

  sub {
    ## For use in guaranteeing the -f command uses the
    ## right path
    my $dir = getcwd;
      if (-f $dir . '/'. $_) {
        ## Determine if base dir matches r1 or r2
        (my $base) = $File::Find::name =~ m/^(\.*\/*\/[^\/]+)\//;
        if ($r1 eq $base) {
          $r1h{$File::Find::name} = $_;
          if ($r1hkcn == 1) {
            print "v:$File::Find::name   k:$_\n";
        } else {
          $r2h{$File::Find::name} = $_;
          if ($r2hkcn == 1) {
            print "v:$File::Find::name   k:$_\n";


my ($r1full,$r1end);
while (($r1full,$r1end) = each(%r1h)) {
  foreach my $key (keys %r2h) {
    if ($r2h{$key} eq $r1end) {
       print "$r2h{$key} MATCHES $r1end\n";

Thread Next Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About