Get index of highest value

Anything beyond the basics in using the LiveCode language. Share your handlers, functions and magic here.

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
xApple
Posts: 113
Joined: Wed Nov 29, 2006 10:21 pm

Get index of highest value

Post by xApple » Thu Apr 26, 2007 10:45 pm

This function (that I wrote) seams to be essential in several of my projects, all though I can't keep thinking there must be a more efficient way of doing it... anyone an idea ?

Code: Select all

function getIndexOfHighestValue numberArray
  -- Example: Input="3,7,5,1"; Output="2"
  put the itemDel into myOldDel
  set the itemDel to comma
  repeat with x = 1 to item 2 of line 1 of extents(numberArray)
    put numberArray[x] & comma & x into line x of numberList
  end repeat
  sort lines of numberList descending numeric by item 1 of each 
  put item 2 of line 1 of numberList into theAnswer
  set the itemDel to myOldDel
  return theAnswer
end getIndexOfHighestValue

Mark Smith
Posts: 179
Joined: Sat Apr 08, 2006 11:08 pm
Contact:

Re: Get index of highest value

Post by Mark Smith » Fri Apr 27, 2007 1:10 am

xApple wrote:This function (that I wrote) seams to be essential in several of my projects, all though I can't keep thinking there must be a more efficient way of doing it... anyone an idea ?
I haven't benchmarked them, but this is what I've come up with:

Code: Select all

function getIndexOfHighestValue numberArray
  put max(numberArray) into tMax
  repeat for each line L in the keys of numberArray
    if numberArray[L] = tMax then return L
  end repeat
end getIndexOfHighestValue
best,

Mark
Last edited by Mark Smith on Fri Apr 27, 2007 1:45 am, edited 1 time in total.

marielle
Livecode Opensource Backer
Livecode Opensource Backer

Re: Get index of highest value

Post by marielle » Fri Apr 27, 2007 1:14 am

Code: Select all

function getIndexOfHighestValue numberItems
  put max(numberItems) into tMax
  put itemOffset("," & tMax & ",", "," & numberItems & ",") into tOff
  return tOff
end getIndexOfHighestValue
If the input must be an array rather than a sequence of items

Code: Select all

function getIndexOfHighestValue arrayItems
  put item 2 of line 1 of extents(arrayItems) into tMax
  combine arrayItems with ","
  put itemOffset("," & tMax & ",", "," & arrayItems & ",") into tOff
  return tOff
end getIndexOfHighestValue
It's not really proposed as an improvement as Mark's solution is very efficient. You will need to benchmark both to get an idea of which one to use in which context.

If you need to know all indexes, not just the first one, then best is to adapt Mark's solution rather than this one. In Mark's code, "if numberArray[L] = tMax then return L " would become "if numberArray[L] = tMax then put L & cr after tList ... end repeat ... return tList"
Last edited by marielle on Fri Apr 27, 2007 6:09 pm, edited 9 times in total.

marielle
Livecode Opensource Backer
Livecode Opensource Backer

Post by marielle » Fri Apr 27, 2007 1:30 am

funny, I found a way to rapidly remove duplicates in a list when playing with this:

Code: Select all

function list.deleteDuplicates pList
   split pList by cr and space
   combine pList by cr and space
   return pList
end list.deleteDuplicates
pList:
3
7
5
1
7
5
6

returns :
1
3
5
6
7

marielle
Livecode Opensource Backer
Livecode Opensource Backer

Re: Get index of highest value

Post by marielle » Fri Apr 27, 2007 1:45 am

xApple wrote:

Code: Select all

  put the itemDel into myOldDel
  ...
  set the itemDel to myOldDel
Note that this is not required. the itemdelimeter is automatically reset to the default (",") when the handler finishes executing. Cf the doc "Since the itemDelimiter is a local property, its value is reset to comma when the current handler finishes executing. It retains its value only for the current handler, and setting it in one handler does not affect its value in other handlers it calls."

xApple
Posts: 113
Joined: Wed Nov 29, 2006 10:21 pm

Re: Get index of highest value

Post by xApple » Fri Apr 27, 2007 6:06 pm

marielle wrote:Note that this is not required. the itemdelimeter is automatically reset to the default (",") when the handler finishes executing. Cf the doc "Since the itemDelimiter is a local property, its value is reset to comma when the current handler finishes executing. It retains its value only for the current handler, and setting it in one handler does not affect its value in other handlers it calls."
Hey thanks, I dind't know that ^^
And yeah thanks both for the faster algorithm, I knew there was a simpler way ! Now I feel stupid I didn't come up with it in the first place : )

marielle
Livecode Opensource Backer
Livecode Opensource Backer

Re: Get index of highest value

Post by marielle » Fri Apr 27, 2007 6:11 pm

xApple wrote:Now I feel stupid I didn't come up with it in the first place : )
Don't feel bad about it. We didn't either come up with these in the first place... we both re-edited the post we initially wrote using the edit button :wink: I used Mark's code to improve mine and he did the same. Consider it collaborative solution finding :-).

Post Reply