[Date Prev][Date Next][Thread Prev][Thread Next] [Search] [Date Index] [Thread Index]

RE: [FWP] Copying hashes



> From: Uri Guttman [mailto:uri@sysarch.com]
> Sent: Sunday, January 30, 2000 14:06
> To: andy@petdance.com
> Cc: fwp@technofile.org; chicagopm@topica.com
> Subject: Re: [FWP] Copying hashes
> 
> 
> >>>>> "AL" == Andy Lester <andy@petdance.com> writes:
> 
>   AL> I'm using the following little snippet of code:
>   AL>     my $r = $sth->fetchrow_hashref() or return 0;
>   AL>     $self->{keys %$r} = $self->{values %$r};
> 
>   AL> I can't imagine there's a better way to plug the values 
> from %$r into my
>   AL> hash, but I figure it's worth asking about...
> 
> well if you call a correct method better, then there is a better way.
> 
> $self->{keys %$r} is a scalar expression with a list key. this will be
> converted into the perl4 style multidimensional array by joining the
> list elements with $; and using that as the single key to the 
> anon hash.
> 
> you want true hash slices:
> 
> @{$self}{keys %$r} = @{$self}{values %$r} ;

You got carried away by what you were correcting.  You don't want to assign
to @$self the elements of @$self whose keys are the values of @$r.  The
correct right-hand side has been posted several times already:

    values %$r

-- 
Larry Rosler
Hewlett-Packard Laboratories
http://www.hpl.hp.com/personal/Larry_Rosler/
lr@hpl.hp.com 

==== Want to unsubscribe from Fun With Perl?  Well, if you insist...
==== Send email to <fwp-request@technofile.org> with message _body_
====   unsubscribe