Reverse the hash and use function each to get each pair : reverse « Hash « Perl






Reverse the hash and use function each to get each pair

   

%presidents = ( George => "A",
                Abe    => "B",
                Thomas => "C",
                Harry  => "D" );
      
@keys = keys( %presidents );
@values = values( %presidents );


print "%presidents with its keys and values reversed\n";
%hash = reverse( %presidents );

while ( ( $key, $value ) = each( %presidents ) ) {
   print "$key => $value\n";
}

   
    
    
  








Related examples in the same category

1.Reverse a hash
2.Reverse a hash: use key as the value, and value as the key
3.Reverse the key and value