--- Mooney Christophe-CMOONEY1 <[EMAIL PROTECTED]> wrote:
> Does anyone know of a slick way to put an array into a hash?
>
> For example, given
>
> %a=
> (
> -a => 1,
> -b => 2,
> -c => 3
> );
> @b=qw/-x 24 -y 25 -z 26/;
>
> Is there a nice way to merge the hash implied by @b into %a to get
>
> %a=
> (
> -a => 1,
> -b => 2,
> -c => 3,
> -x => 24,
> -y => 25,
> -z => 26
> );
Given function pushHash:
sub pushHash (\%@) { # somebody please check that prototyping
local *INHASH = shift;
my %tmpHash = ( @_ );
@INHASH{keys %tmpHash} = values %tmpHash;
}
which would be called with a hash reference and a list to add:
pushHash(\%target, @listToAdd);
or even (I *think*):
pushHash %target, -x => 24, -y => 25, -z => 26;
It would add the new items to the hash.
Personally, if it could be arranged, I'd rather just have a list of
parallel keys and values, and add them thus:
@hash{@keys} = @values;
__________________________________________________
Do You Yahoo!?
Make international calls for as low as $.04/minute with Yahoo! Messenger
http://phonecard.yahoo.com/
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]