Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

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":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (6)
As of 2024-04-20 00:19 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found