Using foreach loops with hashes : Hash Loop « Hash « Perl






Using foreach loops with hashes

   

@opinions = qw( A b c d e f g h jk r e r t r e  e  e e  e  ww );

foreach ( @opinions ) {
   ++$hash{ $_ };
}


# display sorted by frequency in descending order
print "\nWord\tFrequency\n";
print "----\t---------\n"; 

foreach ( sort { $hash{ $b } <=> $hash{ $a } } keys( %hash ) ) {
   print "$_\t", "*" x $hash{ $_ }, "\n";
}

   
    
    
  








Related examples in the same category

1.Iterate hash
2.Loop through an associative array
3.Loop through an associative array While loop
4.Looping Over A Hash
5.While loop and hash key value pair
6.Using while and each function to output value in a hash