arkturuz has asked for the wisdom of the Perl Monks concerning the following question:

Greetings!

For example, I have a list of 5 numbers (/proc/loadavg actually) and I want to store them in a hash %load so that the first load is stored in %load{'ONE'}, second in %load{'FIVE'}, third in %load{'FIFTEEN'}. The rest is discarded.

So, is there a simple way to store a list to a hash which has predefined keys?

Replies are listed 'Best First'.
Re: A hash question
by salva (Canon) on Dec 19, 2005 at 14:47 UTC
    my @loadavg = (0.00, 0.73, 2.86, 1/102, 32469); my %load; @load{qw(ONE FIVE FIFTEEN)} = @loadavg;
      Hm, why didn't I came with that? I am becoming slow... Too much alcohol :-)

      Thnx, perl being :-)

      BTW, shouldn't the last line be like:

      @load{qw(ONE FIVE FIFTEEN)} = (@loadavg[0,1,2]);
        Both ways are valid. The assignment operator silently ignores the extra values.