Harry Putnam wrote:

What I hope to do is compare hashes and a few different ways.

determine what is in one and not in the other for example.

These exmple hashes are supposed to represent file names.

The hashes are created by making the key the full path and file name,
and the value just the end filename

my %h1  = (
           './b/f1'       =>  'f1',
           './b/c/fa'     =>  'fa',
           './b/l/c/f2'   =>  'f2',
           './b/g/f/r/fb' =>  'fb'
);

my %h2  = (
'./b/fb' => 'fb', './b/c/fd' => 'fd',
           './b/l/c/f2'    => 'f2',
           './b/g/f/r/fc'  => 'fc',
           './b/g/h/r/fb'  => 'fb'


I think you want to find filenames that exist in multiple paths.

So you could create a hash like this:

   (
       'f1' => [ 'p1', 'p4' ],
       'f2' => [ 'p1', 'p2', 'p4' ],
       ...
   )

(read f as filename and p as pathname)

In that way, you would have, per filename, a list of all paths.



An example that shows you the commands that exist more than once in your path:

perl -Mstrict -MData::Dumper -MFile::Basename=basename,dirname -wle'
    my %h;
    for my $dir ( split ":", $ENV{PATH} ) {
        push @{ $h{ basename($_) } }, dirname($_) for glob "$dir/*";
    }
    @{ $h{ $_ } } < 2 and delete $h{ $_ } for keys %h;
    print Dumper( \%h );
' |less


Similar without File::Basename

perl -Mstrict -MData::Dumper -wle'
    my %h;
    for my $dir ( split ":", $ENV{PATH} ) {
        m{(.*/)(.*)} and push @{ $h{ $2 } }, $1 for glob "$dir/*";
    }
    @{ $h{ $_ } } < 2 and delete $h{ $_ } for keys %h;
    print Dumper( \%h );
' |less


--
Ruud

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to