JusaEngineer has asked for the wisdom of the Perl Monks concerning the following question:
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
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: counting occurrence of an element in a hash
by davido (Cardinal) on Jun 05, 2021 at 00:51 UTC | |
by JusaEngineer (Novice) on Jun 05, 2021 at 19:04 UTC | |
|
Re: counting occurrence of an element in a hash
by kcott (Archbishop) on Jun 05, 2021 at 04:02 UTC | |
by JusaEngineer (Novice) on Jun 07, 2021 at 17:47 UTC | |
|
Re: counting occurrence of an element in a hash (updated)
by AnomalousMonk (Archbishop) on Jun 05, 2021 at 01:44 UTC | |
by JusaEngineer (Novice) on Jun 07, 2021 at 17:50 UTC |