in reply to Re^2: Not sure how to populate a hash
in thread Not sure how to populate a hash
Hi aditya1977
Lets try this one.
use strict; use warnings; use Data::Dumper; my %nodemetrics; my @nodelist = qw(server1 server2); my @system_attributes = qw(memory cpu disk network); # Initialise the hash with zeros my ($node); foreach $node (@nodelist) { foreach (@system_attributes){ $nodemetrics{$node}{$_} = 0; } } print Dumper(\%nodemetrics); my $string="1024|2200|30|100"; my @data = split(/\|/,$string); my @keys = keys %{$nodemetrics{'server1'}}; my $hashref = $nodemetrics{'server1'}; @$hashref{@keys}=@data; $nodemetrics{'server1'}=$hashref; print Dumper (\%nodemetrics);
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^4: Not sure how to populate a hash
by Monk::Thomas (Friar) on Sep 24, 2014 at 12:02 UTC |