G'day JusaEngineer,

Welcome to the Monastery.

You can collate all the information you want in a single, new hash by iterating the values of %hash and transferring information to the new hash.

In the script below, I've given all of the Descriptions unique values: you can now tell all three "A Description of this button On/Off" apart (ditto for the two with "Momentary").

#!/usr/bin/env perl use strict; use warnings; my %hash = ( 'ScreenName1.Description1' => { 'ScreenName' => 'A', 'Description' => 'Desc1', 'Type' => 'On/Off' }, 'ScreenName2.Description2' => { 'ScreenName' => 'B', 'Description' => 'Desc2', 'Type' => 'Momentary' }, 'ScreenName3.Description3' => { 'ScreenName' => 'A', 'Description' => 'Desc3', 'Type' => 'Momentary' }, 'ScreenName4.Description4' => { 'ScreenName' => 'A', 'Description' => 'Desc4', 'Type' => 'On/Off' }, 'ScreenName5.Description5' => { 'ScreenName' => 'B', 'Description' => 'Desc5', 'Type' => 'On/Off' }, ); my %parsed; for (values %hash) { push @{$parsed{$_->{ScreenName}}}, join ' ', @{$_}{qw{Description Type}}; } print "*** Counts ***\n"; print "$_ - ", 0+@{$parsed{$_}}, "\n" for keys %parsed; print "\n*** New hash (\%parsed) ***\n"; use Data::Dump; dd \%parsed;

Output from a sample run:

*** Counts *** A - 3 B - 2 *** New hash (%parsed) *** { A => ["Desc1 On/Off", "Desc3 Momentary", "Desc4 On/Off"], B => ["Desc2 Momentary", "Desc5 On/Off"], }

Note that hashes are unordered. Here's the output from another sample run:

*** Counts *** B - 2 A - 3 *** New hash (%parsed) *** { A => ["Desc3 Momentary", "Desc1 On/Off", "Desc4 On/Off"], B => ["Desc5 On/Off", "Desc2 Momentary"], }

Depending on how you want to use the data, you may need to use sort in one of more places. For future reference, please note that if you specify requirements you'll generally get better answers: all I have to work with is "something like" for part one, and no information at all for part two.

See also: Data::Dump.

— Ken


In reply to Re: counting occurrence of an element in a hash by kcott
in thread counting occurrence of an element in a hash by JusaEngineer

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.