Count number of times a number appears

Welcome to our brand new Clickteam Community Hub! We hope you will enjoy using the new features, which we will be further expanding in the coming months.

A few features including Passport are unavailable initially whilst we monitor stability of the new platform, we hope to bring these online very soon. Small issues will crop up following the import from our old system, including some message formatting, translation accuracy and other things.

Thank you for your patience whilst we've worked on this and we look forward to more exciting community developments soon!

Clickteam.
  • If i have a set of numbers stored in some alterables values, how would I loop through the values and count the number of times each unique number appears?
    For example:
    Alterable value 0 = 0
    Alterable value 1 = 1
    Alterable value 2 = 1000
    Alterable value 3 = 500
    Alterable value 4 = 1000

    I would want my program to output that there is one 0, one 1, one 500, and two 1000s....

    Thanks!

  • Which platform do you want to do this on? Could you give a little more detail on what you are trying to achieve here at all? I can see how to do what you need, but it doesn't seem like a very efficient solution. Also, what is the range of potential numbers you will be testing for? Is there an upper limit, lower limit, only a set pool of values (such as 100, 200, 300 etc.)

  • OK. Here is the method for 0, 1 and 2s

    Create a counter for 0, 1, 2s etc.

    -> Store the number in a string object, using the "Number to string" expression in the string object expression menu
    * Always
    - Set NumberVersionOfText$ to str$(Number)

    -> Do a fast loop (in the system object)
    * Always
    + Set "number of 0s" to 0
    + Set "number of 1s" to 0
    + Set "number of 2s" to 0
    + Start loop "Count" Len(String$("NumberInText)) (in the system object expression menu)

    * On loop "count"
    * Mid$(NumberVersionOfText, LoopIndex("Count"), 1) = "0"
    - Add 1 to "Number of 0s"

    * On loop "count"
    * Mid$(NumberVersionOfText, LoopIndex("Count"), 1) = "1"
    - Add 1 to "Number of 1s"

    * On loop "count"
    * Mid$(NumberVersionOfText, LoopIndex("Count"), 1) = "2"
    - Add 1 to "Number of 2s"

    This should work.

    Francois
    PM: Please login to see this link.

  • For an arbitrary number of possibilities, I'd create a map (Named Variable Object or anything else that supports key-value pairs) and loop over the list once by using Get Alterable Value with Index N (I can't remember the exact expression):

    - Get the alterable value
    - Increment the value's key in the map by 1

    The map will hold the number of occurrences of each number. In the given example:

    Alterable value 0 = 0
    Alterable value 1 = 1
    Alterable value 2 = 1000
    Alterable value 3 = 500
    Alterable value 4 = 1000

    Loop: Gets 0. Map now contains:
    0=1

    Loop: Gets 1. Map now contains:
    0=1
    1=1

    Loop: Gets 1000. Map now contains:
    0=1
    1=1
    1000=1

    Loop: Gets 500. Map now contains:
    0=1
    1=1
    1000=1
    500=1

    Loop: Gets 1000. Map now contains:
    0=1
    1=1
    1000=2
    500=1

    Now loop over each key in the map, and you have the number of occurrences of that number.

    Please login to see this link.
    Please login to see this link.

Participate now!

Don’t have an account yet? Register yourself now and be a part of our community!