Brainteaser (help!)

Lee Larcombe leelarcombe at hotmail.com
Wed Mar 14 09:32:39 PDT 2007

```Hi All

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.

I thought that I could also turn this into a hash:

%hashG = {a=>1.048, b=>0.657, c=>0.437, d=>2.363, e=>1.964, f=>0.787,
g=>1.078, h=>0.686};

Reverse the hash so that the keys become values and the values keys.

%reverseHashG = {1.048=>a, 0.657=>b, 0.437=>c, 2.363=>d, 1.964=>e,
0.787=>f, 1.078=>g, 0.686=>h};

The take \$arrayG[0] from a sorted array and use it to read the value in the
reversed hash:

\$reverseHashG{\$arrayG[0]}

The I would get the answer 'd' - telling me essentially what I need to know.

However: 1. This seems like a round about way of doing it - shouldn't it be
easier? 2. There might be a case where two of the numbers in the original
array are the same. When the hash is reversed I will lose some of these -
this would be a problem if the two equal values were equal largest, plus how
would the array sort work if there were two equal largest values?

I have tried 'The Internet', a couple of books, and some colleagues here,
but we cannot think of a way to do this. Any insight would be gratefully

Thanks

Lee

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

www.larcombesplace.org.uk

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

```