Shawn H Corey <shawnhco...@gmail.com> writes:

>   my %inv_hash = invert( \%hash );
>   print '%inv_hash: ', Dumper \%inv_hash;
>
> This will output:
>
> %inv_hash: $VAR1 = {
>   'f2' => [
>     './b/l/c/f2'
>   ],
>   'fb' => [
>     './b/fb',
>     './b/g/h/r/fb'
>   ],
>   'fc' => [
>     './b/g/f/r/fc'
>   ],
>   'fd' => [
>     './b/c/fd'
>   ]
> };
>
> Note that both values for fb are preserved.

More questions here:
This inversion technique is being used with hashs originating as hashs
of a directory structure.

Using File::Find, they are build like this:

dir1
#   keys                      values

    h1{$File::Find::name  =>   $_;  

dir2

   h2{$File::Find::name  =>   $_;

I'm using the inversion technique to determine what is in dir1 but not
in dir2.... That's it nothing else

Doesn't matter if there are duplicates or if they really are the same
file with different names etc etc.

All I'm looking for in this bit of code is to print the fullname and
end of path filename of what is in h1 but not h2

You probably already know the output is not going to be what I want.

A small snippet of the output (spacing shrunk a little to avoid
wrapping):

  [...]

  ARRAY(0x91af588)                   convol5.pnm 
  exits only in rh1
  ---   ---   ---
  ARRAY(0x91aeb38)                   .arch-inventory 
  exits only in rh1

  [...]

How can I get the actual name represented by `ARRAY(0x91af588)' etc?
Is Data::Dumper (or some other module) the only way, or can I get at
it with no extra modules from the inverted hash?

-------        ---------       ---=---       ---------      -------- 
[ imagine generation of hashs is already complete since we just load
some from disk below]

------- 8< snip ---------- 8< snip ---------- 8<snip ------- 

#!/usr/local/bin/perl

use strict;
use warnings;
use Storable;

## files on disk with stored hashes
my $h1f = './kp.h1_full_st';
my $h2f = './kp.h2_full_st';

## Retrieve them with Storable module
my %h1  = %{retrieve($h1f)};
my %h2  = %{retrieve($h2f)};

## invert both h1 and h2 hashs by using hash references
## in a sub function
my %inv_h1 = invert( \%h1 );
my %inv_h2 = invert( \%h2 );

## Print only the fullname and end_of_path_name present
## in h1 but not h2
for my $name ( keys %inv_h1 ){
  if ( !  exists $inv_h2{$name} ){
    printf "%-46s %s %s\n",$inv_h1{$name}, $name,"
exits only in h1\n---   ---   ---";
  }
}

## invert a hash... from Shawn C
sub invert {
  my $h = shift @_;
  my %inv = ();

  while( my ( $k, $v ) = each %{ $h } ){
    push @{ $inv{$v} }, $k;
  }
  return %inv;
}




-- 
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