Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Re: Array consolidating values

by BillKSmith (Monsignor)
on Oct 07, 2019 at 19:18 UTC ( [id://11107153]=note: print w/replies, xml ) Need Help??


in reply to Array consolidating values

I think that you want to reorganize an array of hashes into a hash of arrays.
use strict; use warnings; use Data::Dumper; my @servers = ( { Host => 'server02', GroupName => 'serversA' }, { Host => 'server04', GroupName => 'serversA' }, { Host => 'server02', GroupName => 'serversB' }, { Host => 'server04', GroupName => 'serversB' }, { Host => 'server02', GroupName => 'serversC' }, { Host => 'server04', GroupName => 'serversC' }, { Host => 'server02', GroupName => 'serversD' }, ); my %consolidated; foreach (@servers) { push @{$consolidated{$_->{Host}}}, $_->{GroupName}; } print Dumper(\%consolidated);
OUTPUT: $VAR1 = { 'server02' => [ 'serversA', 'serversB', 'serversC', 'serversD' ], 'server04' => [ 'serversA', 'serversB', 'serversC' ] };
Bill

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11107153]
help
Chatterbox?
and the web crawler heard nothing...

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

    No recent polls found