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

Re: [FWP] Closest hash value



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);
>
>An auxilary variable could be used to avoid the extra calculation. Or
>change the initialization to simply set $best to the highest possible
>value.
>
><random thought>
>If Hashes could be accessed like arrays getting the first key could be
>a nice touch here.
></random thought>

You can get the "first" key with

($best) = each %h;


Fergal



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