Re Brainteaser - thanks!

Lee Larcombe leelarcombe at hotmail.com
Thu Mar 15 01:58:30 PDT 2007


Hi

Thanks for the help yesterday. I went with the option of using List::Utils
and List::MoreUtils

> I was wondering if anyone could think of a solution to a problem I have. I
> have a script which contains an array which looks like:
> 
>    @arrayG = qw (1.048 0.657 0.437 2.363 1.964 0.787 1.078 0.686);
> 
> I need to know the array position of the largest number, ie 2.363 is
> largest, but I need to return $arrayG[3] - the '3' is what I need.

This worked as simply:

Use List::Utils qw(max);
Use List::MoreUtils qw(firstidx);

$highest = max @arrayG;
$identifier = firstidx {$_ == $highest} @arrayG;

Very neat - Thanks.

I still don't know how max works when there are two equal values in the
array which are equal largest, but I think I can ignore this for now - it
might never happen.

Lee

============================
Lee Larcombe

www.larcombesplace.org.uk

msn: leelarcombe at hotmail.com
jabber: leelarcombe at jabber.com
ICQ: 381691209
skype: leelarcombe (by prior arrangement)
============================





More information about the MiltonKeynes-pm mailing list