Noting that maikelnight usually provides code with questions, I don't mind cutting a little slack. As 1nickt suggests, iterate over @servers creating a HoA where the key is the "Host" and the value is an anonymous array onto which we push each "GroupName" as it is encountered. I've added another server that is in only one group and also shuffled the @servers array so as to show how to get sorted groups in the output. I also include Data::Dumper output to show the HoA structure. The code:-

use 5.026; use warnings; use Data::Dumper; my @servers = ( { Host => q{server04}, GroupName => q{serversB}, }, { Host => q{server02}, GroupName => q{serversC}, }, { Host => q{server04}, GroupName => q{serversA}, }, { Host => q{server02}, GroupName => q{serversD}, }, { Host => q{server02}, GroupName => q{serversB}, }, { Host => q{server02}, GroupName => q{serversA}, }, { Host => q{server03}, GroupName => q{serversC}, }, { Host => q{server04}, GroupName => q{serversC}, }, ); my %inGroups; foreach my $rhServer ( @servers ) { push @{ $inGroups{ $rhServer->{ Host } } }, $rhServer->{ GroupName + }; } say Data::Dumper ->new( [ \ %inGroups ], [ qw{ *inGroups } ] ) ->Sortkeys( 1 ) ->Dumpxs(); say qq{Server $_ is in groups }, join q{, }, sort @{ $inGroups{ $_ } } for sort keys %inGroups;

The output:-

%inGroups = ( 'server02' => [ 'serversC', 'serversD', 'serversB', 'serversA' ], 'server03' => [ 'serversC' ], 'server04' => [ 'serversB', 'serversA', 'serversC' ] ); Server server02 is in groups serversA, serversB, serversC, serversD Server server03 is in groups serversC Server server04 is in groups serversA, serversB, serversC

I hope this is of interest.

Cheers,

JohnGG


In reply to Re: Array consolidating values by johngg
in thread Array consolidating values by maikelnight

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.