Reverse a hash: use key as the value, and value as the key : reverse « Hash « Perl






Reverse a hash: use key as the value, and value as the key

   


$hash{fruit} = apple;
$hash{sandwich} = hamburger;
$hash{drink} = bubbly;

%reversed = reverse %hash;

foreach $key (sort keys %reversed) {
    print "$key => $reversed{$key}\n";
}

   
    
    
  








Related examples in the same category

1.Reverse a hash
2.Reverse the hash and use function each to get each pair
3.Reverse the key and value