Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Re: Not sure how to populate a hash

by LanX (Saint)
on Sep 24, 2014 at 10:06 UTC ( [id://1101769]=note: print w/replies, xml ) Need Help??


in reply to Not sure how to populate a hash

you could use a hash slice to populate many slots at once

@$hashref{@keys}=@values

With

$hashref=$nodemetrics{$node}

update

Not sure why you prepopulate to 0, simply undef seems more robust and is easily done:

@$hashref{@keys}=()

HTH :)

Cheers Rolf

(addicted to the Perl Programming Language and ☆☆☆☆ :)

Replies are listed 'Best First'.
Re^2: Not sure how to populate a hash
by aditya1977 (Novice) on Sep 24, 2014 at 10:45 UTC
    Sorry Rolf

    I'm afraid I still dont get it. :-(

    By @keys do you mean @system_attributes?

    So I need to do this?

    @$hashref{@system_attributes}=@data;

      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,

        Thanks so much for your help guys. I mostly get this now!

        Just one last question. If I wanted to return the attributes of one of the servers as a hash, how do I get to that data?

        So in other words, I want to a create a simple hash %server_properties which name value pairs of one of the servers?

      Why don't you try and see? :)

      Caution order matters!

      Cheers Rolf

      (addicted to the Perl Programming Language and ☆☆☆☆ :)

      PS: and plz have a look at the links provided.

      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);

      All is well
        my @keys = keys %{$nodemetrics{'server1'}};
        in a somewhat recent perl this leads to:
        $ perl scratch.pl [...] 'server1' => { 'memory' => '1024', 'network' => '2200', 'cpu' => '30', 'disk' => '100' }, [...] $ perl scratch.pl [...] 'server1' => { 'disk' => '1024', 'memory' => '2200', 'cpu' => '30', 'network' => '100' } [...]

        OUCH!!

        You already have the keys in the correct order available, why not use that?

        my @system_attributes = qw(memory cpu disk network); [...] my $hashref = $nodemetrics{'server1'}; @$hashref{@system_attributes}=@data;

Log In?
Username:
Password:

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

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

    No recent polls found