Hello aditya1977,
In the spirit of TMTOWTDI, here’s a concise implementation of LanX’s hash slice solution:
#! perl use strict; use warnings; use Data::Dump; my %nodemetrics; my @nodelist = qw(server1 server2); my @attributes = qw(memory cpu disk network); my $string = '1024|2200|30|100'; my @data = split /\|/, $string; @{ $nodemetrics{$_ } }{@attributes} = (0) x @attributes for @node +list; dd \%nodemetrics; @{ $nodemetrics{server1} }{@attributes} = @data; dd \%nodemetrics;
Output:
22:03 >perl 1027_SoPW.pl { server1 => { cpu => 0, disk => 0, memory => 0, network => 0 }, server2 => { cpu => 0, disk => 0, memory => 0, network => 0 }, } { server1 => { cpu => 2200, disk => 30, memory => 1024, network => 100 + }, server2 => { cpu => 0, disk => 0, memory => 0, network => 0 }, } 22:03 >
Hope that helps,
| Athanasius <°(((>< contra mundum | Iustus alius egestas vitae, eros Piratica, |
In reply to Re^3: Not sure how to populate a hash
by Athanasius
in thread Not sure how to populate a hash
by aditya1977
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |