[Thousand-oaks-pm] Perl hash question

Aran Deltac adeltac at valueclick.com
Fri Dec 2 11:02:06 PST 2005



> -----Original Message-----
> From: ansok at alumni.caltech.edu [mailto:ansok at alumni.caltech.edu]
> Sent: Friday, December 02, 2005 10:48 AM
> To: Aran Deltac
> Cc: Jim Walker; thousand-oaks-pm at mail.pm.org
> Subject: Re: [Thousand-oaks-pm] Perl hash question
> 
> No, I don't think he wanted to preserve existing keys in %foo
> -- he said "%foo takes %bar values for keys defined in %bar".

The full quote is "Where %foo retains it values for keys undefined in
%bar and %foo takes %bar values for keys defined in %bar".  The key part
being "Where %foo retains it values".

> If he did, though, the simple way (perhaps not the best for
> very large hashes) would be:
> 
> %foo = (%bar, %foo);
> 
> Instead of your map statement, I might write it as
> 
> $foo{$_} = $bar{$_} foreach grep !exists $foo{$_}, keys %bar;

Oddly, you grep example and my map one are the exact same number of
characters.  :)

I did a benchmark and it looks like your method with grep is slightly
faster.  I was only able to see a speed increase after running each
method 10,000 times, so it's a minimal speed difference.

Aran

> I don't think there's going to be a significant performance
> difference between the two, though.
> 
> Gary Ansok
> 
> Aram Deltac wrote:
> > Well, he wanted to preserve any existing keys in %foo, so that would
be:
> >
> > foreach (keys %bar) {
> > 	$foo{$_} = $bar{$_}
> > 		unless(exists $foo{$_});
> > };
> >
> > Personally I like using map for things like this:
> >
> > map { $foo{$_}=$bar{$_} unless(exists $foo{$_}) } keys(%bar);
> >
> > Aran
> >
> >> -----Original Message-----
> >> From: thousand-oaks-pm-bounces+adeltac=valueclick.com at pm.org
> >> [mailto:thousand-oaks-pm-bounces+adeltac=valueclick.com at pm.org] On
> > Behalf
> >> Of Jim Walker
> >> Sent: Friday, December 02, 2005 9:49 AM
> >> To: thousand-oaks-pm at mail.pm.org
> >> Subject: Re: [Thousand-oaks-pm] Perl hash question
> >>
> >> And, for large hashes,
> >>
> >> foreach (keys %bar) {$foo{$_} = $bar{$_}};
> >>
> >> should use less memory.
> >> --
> >> Jim
> >>
> >>
> >>
> >> Gary Ansok wrote:
> >>
> >> >Robert Hull wrote:
> >> >
> >> >
> >> >
> >> >>I'm looking for a succinct way to join two hashes.  What I would
> > like
> >> is:
> >> >>
> >> >>%foo .= %bar ;
> >> >>
> >> >>Where %foo retains it values for keys undefined in %bar
> >> >>and
> >> >>%foo takes %bar values for keys defined in %bar.
> >> >>
> >> >>
> >> >>
> >> >>
> >> >I believe that
> >> >
> >> >%foo = (%foo, %bar);
> >> >
> >> >will do what you want.
> >> >
> >> >Gary Ansok
> >> >
> >> >_______________________________________________
> >> >Thousand-oaks-pm mailing list
> >> >Thousand-oaks-pm at pm.org
> >> >http://mail.pm.org/mailman/listinfo/thousand-oaks-pm
> >> >
> >> >
> >> >
> >> >
> >> _______________________________________________
> >> Thousand-oaks-pm mailing list
> >> Thousand-oaks-pm at pm.org
> >> http://mail.pm.org/mailman/listinfo/thousand-oaks-pm
> >
> > _______________________________________________
> > Thousand-oaks-pm mailing list
> > Thousand-oaks-pm at pm.org
> > http://mail.pm.org/mailman/listinfo/thousand-oaks-pm
> >




More information about the Thousand-oaks-pm mailing list