The standard way would be to just run through the list and count (untested):

on HowMany theList, theValueToFind
  nTimes = 0
  nItems = count(theList)
  repeat with i = 1 to nItems
     if theList[i] = theValueToFind then
        nTimes = nTimes + 1
     end if
  end repeat
  return nTimes
end

But if you are need to do this for many different values, then perhaps you could scan the list once, and build up a property list made up of [<someValue1>:<countOfSomeValue1>, <someValue2>:<countOfSomeValue2> ...]

Again, untested, but here's the general idea:


global gOriginalList  -- linear list
global gCountList  -- property list

-- Generate your gOriginal list however you do it

on GenerateCountList
  gCountList = [:]
  nItems = count(gOriginalList)
  repeat with iOriginalListIndex = 1 to nItems
    originalValue = gOriginalList[iOriginalListIndex]
    currentCount = getAProp(gCountList, originalValue)
    if voidp(currentCount) then  -- this value hasn't been seen before
       addProp(gCountList, originalValue, 1)    -- set the count to one

   else -- already seen, add one to it
        currentCount = currentCount + 1
        gCountList[originalValue] = currentCount
   end if
end


Given this list when you want to find out how many of any value there are in your list, you just need a quick routine like this:

on GetCount valueIn
  theCount = getAProp(gCountList, valueIn)
  if voidp(theCount) then -- not found
      theCount = 0
  end if
  return theCount
end

Hope this helps,

Irv



At 3:09 PM -0300 6/30/05, Rods wrote:
Hi,

How is the best way to count the occurrences of values in a list? I have a
list with 6000+ random generated numbers and I need to count how many times
each of one appear.

Thank's in advance



Rodrigo Peres

[To remove yourself from this list, or to change to digest mode, go to http://www.penworks.com/lingo-l.cgi To post messages to the list, email lingo-l@penworks.com (Problems, email [EMAIL PROTECTED]). Lingo-L is for learning and helping with programming Lingo. Thanks!]


--

Multimedia Wrangler.
[To remove yourself from this list, or to change to digest mode, go to 
http://www.penworks.com/lingo-l.cgi  To post messages to the list, email 
lingo-l@penworks.com  (Problems, email [EMAIL PROTECTED]). Lingo-L is for 
learning and helping with programming Lingo.  Thanks!]

Reply via email to