The main issue with the code you've shown is that in the three places you're doing something like $HashCount{$Screenkey} = {$i => "..."}, you're replacing what's stored in $HashCount{$Screenkey} with a new reference to a new anonymous hash, instead of adding something to the existing hash. You can delete the line $HashCount{$Screenkey} = $Screenkey; since that value is going to get replaced anyway. Then, you can replace the three aforementioned hash accesses with $HashCount{$Screenkey}{0} and $HashCount{$Screenkey}{$i} respectively - what this does is take advantage of a Perl feature called autovivification, which means that simply accessing $HashCount{$Screenkey} as if it were a reference to a hash causes a new anonymous hash to be created for you.
I think you've also got a logic issue with $i - in the case that $Screenkey ne $ScreenkeyOld, I am guessing that you probably want to start the numbering over - but you're only resetting $i=1 after already having used its previous value. The same goes for $i++. If I fix this, the next issue is that I think you've got a mistake in your sample data: since you're iterating over the sorted keys of %hash, Alarms will come first, but in your sample data you seem to expect the order to be Trend to USB, Alarms, and Flow Diagram. I haven't fixed this issue in the code below.
In general, note that you have two unused variables, @List and the outer $Screenkey, since you're only using the second $Screenkey defined inside the loop. Also, your code would benefit from better indentation - perltidy can help with that. And you can do away with %ScreenNameCount, since you can do that operation directly in %HashCount.
Putting all of that together (as mentioned above the test will fail since I haven't changed the order of the expected output you showed in your question):
use warnings; use strict; my %hash = ( 'Alarms.Alarm Acknowledge' => { 'ScreenName' => 'Alarms', 'Description' => 'Alarm Acknowledge', 'Type' => 'On/Off', }, 'Flow Diagram.Sequence Hold' => { 'ScreenName' => 'Flow Diagram', 'Description' => 'Sequence Hold', 'Type' => 'Momentary', }, 'Flow Diagram.Sequence Advance' => { 'ScreenName' => 'Flow Diagram', 'Description' => 'Sequence Advance', 'Type' => 'Momentary', }, 'Flow Diagram.Sanitize Enable/Disable' => { 'ScreenName' => 'Flow Diagram', 'Description' => 'Sanitize Enable/Disable', 'Type' => 'On/Off', }, 'Trend to USB.Start/Stop Trend Toggle' => { 'ScreenName' => 'Trend to USB', 'Description' => 'Start/Stop Trend Toggle', 'Type' => 'On/Off', }, 'Alarms.Alarm Reset' => { 'ScreenName' => 'Alarms', 'Description' => 'Alarm Reset', 'Type' => 'On/Off', }, 'Alarms.Alarm Trigger' => { 'ScreenName' => 'Alarms', 'Description' => 'Alarm Trigger', 'Type' => 'Momentary', }, ); my %HashCount; $HashCount{ $hash{$_}{ScreenName} }{0}++ foreach keys %hash; my $ScreenkeyOld = 'Old'; my $i = 1; foreach my $key ( sort keys %hash ) { my $Screenkey = $hash{$key}{ScreenName}; my $Description = $hash{$key}{Description}; my $Type = $hash{$key}{Type}; if ( $Screenkey eq $ScreenkeyOld ) { $i++; #print "$Screenkey <$i>\n"; #Debug $HashCount{$Screenkey}{$i} = "\\t \\t \\t \\t\& \\mytabhead{$D +escription} \\t\& $Type \\t\\\\ \\hline \\n $i - $Screenkey - $Screen +keyOld"; } else { $i = 1; #print "$ScreenkeyOld -> $Screenkey <$i>\n"; #Debug $HashCount{$Screenkey}{$i} = "\\mytabhead{$Screenkey} \\t\& \\ +mytabhead{$Description} \\t\& $Type \\t\\\\ \\hline \\n $i - $Screenk +ey - $ScreenkeyOld"; } $ScreenkeyOld = $Screenkey; } use Test::More tests=>1; is_deeply \%HashCount, { 'Alarms' => { '0' => 3, '1' => '\\mytabhead{Alarms} \\t& \\mytabhead{Alarm Trigger} \ +\t& Momentary \\t\\\\ \\hline \\n 1 - Alarms - Trend to USB', '2' => '\\t \\t \\t \\t& \\mytabhead{Alarm Acknowledge} \\t& O +n/Off \\t\\\\ \\hline \\n 2 - Alarms - Alarms', '3' => '\\t \\t \\t \\t& \\mytabhead{Alarm Reset} \\t& On/Off +\\t\\\\ \\hline \\n 3 - Alarms - Alarms', }, 'Flow Diagram' => { '0' => 3, '1' => '\\mytabhead{Flow Diagram} \\t& \\mytabhead{Sequence A +dvance} \\t& Momentary \\t\\\\ \\hline \\n 1 - Flow Diagram - Alarms' +, '2' => '\\t \\t \\t \\t& \\mytabhead{Sequence Hold} \\t& Momen +tary \\t\\\\ \\hline \\n 2 - Flow Diagram - Flow Diagram', '3' => '\\t \\t \\t \\t& \\mytabhead{Sanitize Enable/Disable} +\\t& On/Off \\t\\\\ \\hline \\n 3 - Flow Diagram - Flow Diagram', }, 'Trend to USB' => { '0' => 1, '1' => '\\mytabhead{Trend to USB} \\t& \\mytabhead{Start/Stop +Trend Toggle} \\t& On/Off \\t\\\\ \\hline \\n 1 - Trend to USB - Old' +, }, } or diag explain \%HashCount;
In reply to Re: Hash Manipulation
by haukex
in thread Hash Manipulation
by JusaEngineer
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |