arunshankar.c has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks,

Lets consider below as arrays stored for Keys in hash.

%HoA = ( flintstones => [ "fred", "barney" ], jetsons => [ "george", "fred", "elroy" ], simpsons => [ "fred", "marge", "bart" ], );

I need the duplicates present in the arrays. How can I get the output as below :

HoA{flintstones}{0} = fred HoA{jetsons}{1} = fred HoA{simpsons}{0} = fred

Replies are listed 'Best First'.
Re: get duplicates in hash array
by davido (Cardinal) on Dec 17, 2011 at 21:11 UTC

    Hi arunshankar.c. Before I take the time away from my work and my kids to work your problem for you, could you show me what steps you have already taken to solve your problem yourself?

    If you're totally stuck and don't know where to begin, you should at least read the following:

    After spending an hour reading those and a half hour working on a solution yourself you couldn't possibly be totally stuck without having at least some code attempt to show for it. By showing us at what point you're stuck, we're able to help you over the hump. But by presenting us with a problem where you make no effort to show us where you are currently in the process we're left to assume that you would be better served by posting a job to a freelance site.

    Refer to MJD's Good Advice and Maxims for Programmers: Lines #11095#11905, #11913, #11927, #11928, #11929, #11930, #11949, and #11957.


    Dave

      ??11095??
        #11925
      Hi Dave, Sorry for not coming up with a code worked out by me before posting here. I will make this a point from next time. Thanks, Arun
Re: get duplicates in hash array
by NetWallah (Canon) on Dec 17, 2011 at 23:07 UTC
    Here is one way of doing it - output format is different than the OP requested, but IMHO, this is easier for human consumption:
    use strict; use warnings; my %HoA = ( flintstones => [qw/fred barney george/], jetsons => [qw/george fred elroy/], simpsons => [qw/fred marge bart barney/] ); my %d2; for my $k(keys %HoA){ for (@{$HoA{$k}}){ push @{$d2{$_}}, $k; } } for my $k(sort keys %d2){ next unless $#{$d2{$k}} >0; print "$k:\t (", join (",",sort @{$d2{$k}}),")\n"; }
    Output:
    barney: (flintstones,simpsons) fred: (flintstones,jetsons,simpsons) george: (flintstones,jetsons)

                "XML is like violence: if it doesn't solve your problem, use more."

      Thank you NetWallah, It worked very well :)
Re: get duplicates in hash array
by TJPride (Pilgrim) on Dec 17, 2011 at 21:35 UTC
    These are fun puzzles, I can't resist.

    To the OP - just keep in mind that if this is a Perl homework assignment, your professor is probably trolling the site and will notice if you use this code. Otherwise, have fun with it.

    use strict; use warnings; my %HoA = ( flintstones => [qw/fred barney george/], jetsons => [qw/george fred elroy/], simpsons => [qw/fred marge bart barney/] ); my (%dupes, $k1, $k2, $i); ### Count instances of each word. ### I'm assuming, for the sake of argument, that there ### are no duplicates inside each array. for $k1 (keys %HoA) { $dupes{$_}++ for @{$HoA{$k1}}; } ### For each duplicated word for $k2 (sort keys %dupes) { next if $dupes{$k2} < 2; ### Output matches for $k1 (sort keys %HoA) { for ($i = 0; $i <= $#{$HoA{$k1}}; $i++) { print "\$HoA{'$k1'}[$i] = '$k2';\n" if $HoA{$k1}[$i] eq $k2; } } print "\n"; }
      Thank you, both the logic worked very well