This is a quick example of the process that I had in mind as described in my previous post:
use strict; use warnings; use feature qw /say/; my %hash = (a => 1, b => 2, c => 3, d => 2, e => 5, f => 3); my %HoA; for my $key (keys %hash) { my $value = $hash{$key}; push @{$HoA{$value}}, $key; } for my $key2 (keys %HoA) { my @keys = @{$HoA{$key2}}; say "Keys @keys have the same value: $key2 "; }
Which produces the following output:
$ perl reverse_hash.pl Keys a have the same value: 1 Keys c f have the same value: 3 Keys b d have the same value: 2 Keys e have the same value: 5
The formatting is not exactly what you wanted, but you have the basic algorithm and the results seem correct, I leave it to you to format the output according to your needs. Don't hesitate to ask if you have any further difficulty.

Just in case you need it for better understanding, this is what the HoA looks like at the end of the first loop populating it:

0 HASH(0x60025cbb8) 1 => ARRAY(0x60006f210) 0 'a' 2 => ARRAY(0x600462490) 0 'b' 1 'd' 3 => ARRAY(0x60048d1b8) 0 'c' 1 'f' 5 => ARRAY(0x600495808) 0 'e'


In reply to Re: compare text file and print occurrence of key and value by Laurent_R
in thread compare text file and print occurrence of key and value by waytoperl

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.