in reply to Populating a hash from array

I was just thinking about hash slices the other day, and wondering: what is the syntax for slicing a hash -reference-? I always end up doing things like

($hashref->{key1}, $hashref->{key2}) = @values;

or

{map {$keynames[$_]=>$values[$_]} 0 .. $#values};

which seems much less elegant.

Replies are listed 'Best First'.
Re: Re: Populating a hash from array
by runrig (Abbot) on Jan 05, 2004 at 22:06 UTC
    what is the syntax for slicing a hash -reference-?
    @{$href}{@keys}
      If the reference is a simple scalar, you can just say @$href{@keys}. The curly braces around the reference are only necessary if the reference is a more complicated expression (e.g. @{gimme_href()}{@keys}).

      Same thing goes with using a slice or $# (last index) on an array reference: @$aref[@indices] and $#$aref or @{gimme_aref()}[@indices] and $#{gimme_aref()}.

      Of course, if the curlies make it more clear to you, by all means use them.

Re^2: Populating a hash from array (rqr)
by tye (Sage) on Jan 06, 2004 at 07:33 UTC