in reply to Re: Not sure how to populate a hash
in thread Not sure how to populate a hash

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;

Replies are listed 'Best First'.
Re^3: Not sure how to populate a hash
by Athanasius (Archbishop) on Sep 24, 2014 at 12:05 UTC

    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?

        This creates a copy (NOT an alias i.e. writing to it won't change the original data)

         %server_properties = %{$nodemetrics{server1}}

        Otherwise

        $h_server1 = $nodemetrics{server1}

        (like already shown) is the same hashref, which can be safely changed

        $h_server1->{cpu}=100 or alternatively $$h_server1{cpu}=100

        see perlref and perldsc for details!

        Cheers Rolf

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

Re^3: Not sure how to populate a hash
by vinoth.ree (Monsignor) on Sep 24, 2014 at 11:50 UTC

    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;
Re^3: Not sure how to populate a hash
by LanX (Saint) on Sep 24, 2014 at 10:50 UTC
    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.