Perl Hash Operation

From GM-RKB
Jump to navigation Jump to search

A Perl Hash Operation is a Perl-based associative array operation for a Perl hash.



References

2011

  • (Melli, 2011-09-01) ⇒ Gabor Melli. (2011). “Perl Hash Examples.".

Define a hash

   my %coins = (
      "Quarter", 25,
      "Dime", 10,
      "Nickel", 5) ;

    my %planetMass= (
        mercury ⇒ 0.055,
        venus   ⇒ 0.86,
        earth   ⇒ 1.0,
        mars    ⇒ 0.11,
        jupiter ⇒ 318,
    );
Number of members
    my $size = scalar(keys %planetMass);
Iterate through
   while (my ($key, $value) = each(%coins)){
     ...
   }

   while (my ($key, $value) = each(%$hash_ref) ) {
     ...
   }
Sort a hash
  • using an in-line function
    # largest to smallest
    @array = reverse sort { $hash{$a} <=> $hash{$a} } keys %hash;
Sort a hash - using a subroutine
    foreach $key (sort by_value keys %ary) {
        print $key, '=', $ary{$key}, "\n";
    } 
    sub by_value { $ary{$a} cmp $ary{$b}; }
Sort a hash - descending value through subroutine
    sub by_value { $ary{$b} <=> $ary{$a}; }
Union of Two Hashes
    sub union { return { map { %$_ } @_ } }
  • or
    sub union { +{ map { %$_ } @_ } }
Print a hash
   print %coins;
   while (my ($key, $value) = each(%coins)){
      print "$key, $value\n";
   }
   print map { "$_ ⇒ $hash{$_}\n" } keys %hash;
print by interpolating the hash into an array
   print "@{[ %hash ]}\n";
print a temporary array variable
   my @temp = %hash;
   print "@temp";
print a hash reference
 
   print "the keys... “, sort keys %$hash_ref, "...\n";

Delete an element pair.

   delete($coins{Penny});
   delete($coins{HalfDollar});

Copy a hash


   my %hash_copy = %hash;  # copy a hash

   my $href_copy = $href;  # copy a hash ref

   # pick the keys/values to copy
   my %newRecord_ ;
   @newRecord_{@OntRecordAttrs} = $RKBRecord[$i]{@OntRecordAttrs} ;

Use hash references

    sub foo
    {
        my $hash_ref;
 
        $hash_ref->{ 'key1' } = 'value1';
        $hash_ref->{ 'key2' } = 'value2';
        $hash_ref->{ 'key3' } = 'value3';
 
        return $hash_ref;
    }
 
    my $hash_ref = foo();

Maximum key

 my $max_ = (sort {$h{$b} <=> $h{$a}} keys %h)[0]
 print $max_ ;
 my %hashName = (a ⇒ -1, b ⇒ -2, c ⇒ -3);
 my $max_ = $hash{each %hashName};
 $_ > $max and $max = $_ for values %hashName;
 print $max_ ;
 use List::Util;
 $max_ = $h{ max values %h };
 print $max_ ;

All keys having max value:
  my $max = [sort {$b<=>$a} values %h]->[0] ;
  my @List = map { ($h{$_} == $max) ? $_ : () } keys %h ;

or

sub largest_value_mem (\%) {
    my $hash   = shift;
    my ($key, @keys) = keys   %$hash;
    my ($big, @vals) = values %$hash;

    for (0 .. $#keys) {
        if ($vals [$_] > $big) {
            $big = $vals [$_];
            $key = $keys [$_];
        }
    }
    $key
}

print largest_value_mem %hash; # prints 'largest'

REF: http://stackoverflow.com/questions/2886872/what-is-the-easiest-way-to-get-a-key-with-the-highest-value-from-a-hash-in-perl