On 5/9/10 Sun  May 9, 2010  1:02 PM, "Harry Putnam" <rea...@newsguy.com>
scribbled:

> 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.
> 
> I hadn't caught that.  But that is really nifty.
> 
> If its not to much to ask, how would one get at both of those?

Let $inverted_key have a value from the original hash (e.g., 'fb'). Then the
keys of the original hash that have a value of 'fb' can be fetched into an
array with the following (untested):

my @original_keys = @{$inv_hash{$inverted_key}};

The element of the inverted hash is an array reference. The array is fetched
(copied into @original_keys) by de-referencing the reference. If there was
only key with the value $invereted_key, then the array @original_keys will
have only one element.




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