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

RE: [FWP] Closest hash value



> From: Chaim Frenkel [mailto:chaimf@pobox.com]
> Sent: Monday, July 19, 1999 09:17
> To: Fergal Daly
> Cc: Edward M. Perry; fwp@technofile.org
> Subject: Re: [FWP] Closest hash value
> 
> >>>>> "FD" == Fergal Daly <fergal@esatclear.ie> writes:
> 
> FD> At 15:29 16/07/99 -0400, Chaim Frenkel wrote:
> >> Since you want to avoid the memory for copies how about just
walking
> >> the keys?
> 
> >> do { $best = $_; last} for keys %h;
> >> do { $best = $_ if abs($_ - $key) < abs($best - $key) } for (keys
%h);
> 
> FD> You can get the "first" key with
> 
> FD> ($best) = each %h;
> 
> I know, but I'm not sure if that is optimized. It may cause all
> 1_000_000 keys to be copied to the stack and then the first item
> removed.
> 
> Anyone know for sure?

You have it backwards.  'for keys %h' creates a list of all the keys.
'each %h' returns one key-value pair, and stores the value of an
iterator for succeeding calls.

-- 
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