Return Total of a Given Repeated Number in a List

Andreas Bergendal andreas.bergendal at gmail.com
Fri May 24 02:55:16 EDT 2024


Another way to do it is this:

function countOccurrences pString
   repeat for each item tItem in pString
      add 1 to tFrequencyArray[tItem]
   end repeat
   
   return tFrequencyArray
end countOccurrences

To get the number of 2’s, just extract tFrequencyArray[2] etc.
The advantage is that you get a frequency list (array) of ALL the items in one go.

The bonus advantage is that you also get a list of unique values, by getting the keys of tFrequencyArray.
So it also serves as a duplicate-clearing function, which is sometimes handy.

/Andreas


> 24 maj 2024 kl. 07:08 skrev Roger Guay via use-livecode <use-livecode at lists.runrev.com>:
> 
> Very clever, Mike. I never thought to use itemDelimiter this way. I’ll have to play with this.
> 
> Thanks very much,
> 
> Roger
> 
>> On May 23, 2024, at 7:37 PM, Mike Kerner via use-livecode <use-livecode at lists.runrev.com> wrote:
>> 
>> just spitballing:
>> let's call our containers "tSearchingFor" and "tSearchString"
>> 
>> *set* the itemDelimiter to comma & tSearchString & comma
>> 
>> *put* the number of items in tSearchingFor - 1 into tNum
>> 
>> *if* tSearchString begins with tSearchingFor & comma *then* *add* 1 to tNum
>> 
>> *if* tSearchString ends with comma & tSearchingFor *then* *add* 1 to tNum
>> 
>> On Thu, May 23, 2024 at 10:08 PM Roger Guay via use-livecode <
>> use-livecode at lists.runrev.com> wrote:
>> 
>>> 
>>> Hi all,
>>> 
>>> Please, what’s the easiest way to return the total number of a given
>>> repeated number in a list of numbers? IOW, how many times is 2 repeated in
>>> a list containing 1,2,3,2,4,2,5,2,8 etc. Appreciate your help.
>>> 
>>> 
>>> Thanks,
>>> 
>>> Roger
>>> _______________________________________________
>>> use-livecode mailing list
>>> use-livecode at lists.runrev.com
>>> Please visit this url to subscribe, unsubscribe and manage your
>>> subscription preferences:
>>> http://lists.runrev.com/mailman/listinfo/use-livecode
>>> 
>> 
>> 
>> -- 
>> On the first day, God created the heavens and the Earth
>> On the second day, God created the oceans.
>> On the third day, God put the animals on hold for a few hours,
>>  and did a little diving.
>> And God said, "This is good."
>> _______________________________________________
>> use-livecode mailing list
>> use-livecode at lists.runrev.com
>> Please visit this url to subscribe, unsubscribe and manage your subscription preferences:
>> http://lists.runrev.com/mailman/listinfo/use-livecode
> 
> 
> _______________________________________________
> use-livecode mailing list
> use-livecode at lists.runrev.com
> Please visit this url to subscribe, unsubscribe and manage your subscription preferences:
> http://lists.runrev.com/mailman/listinfo/use-livecode



More information about the use-livecode mailing list