Any help would be greatly appreciated,
Here is an example hash of the same format I'm using. I've been trying the past three days to figure out how to count the times that "ScreenName" equals "A".
I need something like:
A - 3
B - 2
I would also like to know how I could make a new hash out of this one that has "ScreenName" as the $key and "Description" . " " . "Type" as the elements.
use warnings; use strict; use Data::Dumper; my %hash = ( 'ScreenName1.Description1' => { 'ScreenName' => 'A', 'Description' => 'A Description of this button', 'Type' => 'On/Off' }, 'ScreenName2.Description2' => { 'ScreenName' => 'B', 'Description' => 'A Description of this button', 'Type' => 'Momentary' }, 'ScreenName3.Description3' => { 'ScreenName' => 'A', 'Description' => 'A Description of this button', 'Type' => 'Momentary' }, 'ScreenName4.Description4' => { 'ScreenName' => 'A', 'Description' => 'A Description of this button', 'Type' => 'On/Off' }, 'ScreenName5.Description5' => { 'ScreenName' => 'B', 'Description' => 'A Description of this button', 'Type' => 'On/Off' }, ); print Dumper(%hash);
The closest I've gotten so far is something similar to what's below but I can't see to figure out how to compare the actual $hash{$tag}{'ScreenName'} The commented out section is some things I was trying that didn't work.
for $key (sort keys %hashc) { print "$key: \n"; for $ele (keys %{$hash{$key}}) { # my $p=0; # #if ($ele{'ScreenName'} =~ /A/){ # $p++; # } else { # #print "\n $ele{'ScreenName'} --- $p \n"; # $p=0; # } print " $ele: \t\t" . $hash{$key}->{$ele} . "\n"; } }
Thank you, Mel
In reply to counting occurrence of an element in a hash by JusaEngineer
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |