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

Re: Inverting a hash

Thread Previous | Thread Next
From:
Shawn H Corey
Date:
May 9, 2010 09:11
Subject:
Re: Inverting a hash
Message ID:
4BE6DE93.1080102@gmail.com
Brian wrote:
> 
>> I have an example from Shawn C. from another thread, presented here
>> out of context.  The code does just what he meant it to do.
>> It inverts a hash.
> 
> Just curious if there is a particular reason Shawn did not use the reverse function:
> 
> my %hash  = (
>           './b/fb'        => 'fb',
>           './b/c/fd'      => 'fd',
>           './b/l/c/f2'    => 'f2',
>           './b/g/f/r/fc'  => 'fc',
>           './b/g/h/r/fb'  => 'fb'
> 
>       );
> 
> my %inv_hash = reverse %hash;
> 
> 
> 

my %inv_hash = reverse %hash;
print '%inv_hash: ', Dumper \%inv_hash;

Output:
%inv_hash: $VAR1 = {
   'f2' => './b/l/c/f2',
   'fb' => './b/fb',
   'fc' => './b/g/f/r/fc',
   'fd' => './b/c/fd'
};

Note that fb has only one value.


Output of invert():
%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'
   ]
};

Both values of fb are preserved.


-- 
Just my 0.00000002 million dollars worth,
   Shawn

Programming is as much about organization and communication
as it is about coding.

I like Perl; it's the only language where you can bless your
thingy.

Eliminate software piracy:  use only FLOSS.

Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About