Search a table for number of times a value occurs within that table

AppulocityAppulocity Member, PRO Posts: 46

I'm messing around with tables, and I'm trying to make a search for a particular value, but instead of it outputting the row that it occurs in, I would like for it to output the number of times the key appears in the table. For example if I wanted to analyse the results of a survey and I wanted to give the number of people that gave a certain answer. I may well be missing something obvious, but I'm new to tables.

Thanks

Comments

  • tatiangtatiang Member, Sous Chef, PRO, Senior Sous-Chef Posts: 11,949

    You can use them Loop over Table behavior with the index set to self.row (integer) and within the loop have When numeric (or text) expression tableCellValue(tableName,self.row,1) = [some value], change attribute self.foundCount to self.foundCount+1.

    New to GameSalad? (FAQs)   |   Tutorials   |   Templates   |   Greenleaf Games   |   Educator & Certified GameSalad User

  • AppulocityAppulocity Member, PRO Posts: 46

    @tatiang Thanks! That is really helpful. Is there any way to make it auto update?

  • tatiangtatiang Member, Sous Chef, PRO, Senior Sous-Chef Posts: 11,949
    edited January 2015

    By auto-update do you mean that if a table cell value changes, you'll still have an accurate count? No, you'd have to run the loop again or continuously. Something to think about is leveraging the table and text functions (specifically things like tableMergeValues, textReplaceAll, and textLength) to count duplicates without even using a loop.

    While this certainly isn't simple to set up, it can be very powerful and efficient. The idea would be to merge all rows (or columns) into a text string, count the length of the string, replace all occurrences of a search string with an empty character ("") and then count the length of the string again and calculate the difference. The difference would be equal to the number of occurrences of the search string. You could constrain an integer attribute to that expression and it would constantly update.

    Edit: this would work for single digit values but not for varying digit lengths or text strings. For those to work, you'd have to count the number of separators used with the tableMergeValues function and somehow remove the separator when you remove the search string each time. So instead of search for "monkey" you would search for "$monkey" if your separator character was "$". Like I said, complex.

    I feel like @Armelline or @Braydon_SFX may have made a demo that counts frequency so I've notified them.

    New to GameSalad? (FAQs)   |   Tutorials   |   Templates   |   Greenleaf Games   |   Educator & Certified GameSalad User

  • AppulocityAppulocity Member, PRO Posts: 46

    Ah I see, I see. Hmm, typical that I choose to do something very complicated without knowing it haha

  • Braydon_SFXBraydon_SFX Member, Sous Chef, Bowlboy Sidekick Posts: 9,273

    Yes, here's a video tutorial I did a few months back:

  • AppulocityAppulocity Member, PRO Posts: 46
    edited January 2015

    Thanks guys, got that all working. I do however need it to be continuous. How would I go about leveraging the table then? The values are just 1 digit. @Braydon_SFX do you happen to have a tutorial for that?

  • tatiangtatiang Member, Sous Chef, PRO, Senior Sous-Chef Posts: 11,949
    edited January 2015

    I may have spoken too soon. I realized it would be easy (enough) to check for the presence of a binary situation (1010011) by using the sum of the merged cells but for something even a little more complex (131441), it wouldn't work. Hmm...

    New to GameSalad? (FAQs)   |   Tutorials   |   Templates   |   Greenleaf Games   |   Educator & Certified GameSalad User

  • tatiangtatiang Member, Sous Chef, PRO, Senior Sous-Chef Posts: 11,949
    edited January 2015

    Using @Braydon_SFX's method (Loop over Table), you'd need to put that inside a Timer or a custom loop (not the Loop behavior).

    New to GameSalad? (FAQs)   |   Tutorials   |   Templates   |   Greenleaf Games   |   Educator & Certified GameSalad User

  • AppulocityAppulocity Member, PRO Posts: 46

    I tried putting it in a timer, but it just kept increasing as opposed to updating

  • tatiangtatiang Member, Sous Chef, PRO, Senior Sous-Chef Posts: 11,949

    When do you need to update the frequency count? Is it only after someone completes the survey? In other words, at what point(s) in your app would someone be able to change the table data? Because that's when you would want to run the Loop over Table, rather than having an endless timer or loop for that.

    New to GameSalad? (FAQs)   |   Tutorials   |   Templates   |   Greenleaf Games   |   Educator & Certified GameSalad User

  • AppulocityAppulocity Member, PRO Posts: 46

    Well no, the survey thing was just an example. What I'm actually using it for is a poker hand strength calculator (a tool as opposed to a game). You change the value of each of the cards, and then using this table search it would judge how many of each card value are present and based on that allocate the strength. eg. if there were three aces it would see that and show that it's a three of a kind. I'd like for it to be in real time such that as soon as you give a card a value it updates the whole system

  • tatiangtatiang Member, Sous Chef, PRO, Senior Sous-Chef Posts: 11,949

    Sometimes it's best to start with the actual thing you need to make rather than a similar example (unless of course you're not wanting to share a particular game design idea, which I certainly understand!). Because while trying to help you, my mind is going through a process like this: "How would I check the frequency of survey answers constantly? Okay, I might use text strings and text functions... oh but I can't easily constrain those values. Oh wait, a single digit! That's easier. I guess the survey data is just single digits but that still doesn't answer my curiosity about why the survey has to update constantly. Why not just use a 'submit' button and do it then? Oh, I see, it's for poker hands. Then you would need it to update constantly or at least each time a card is played or drawn."

    I'm not picking on you, it's just that it makes it easier to help you when we have more of the details. Sometimes simplifying the question can make it harder to get to the answer.

    Someone with a lot of experience with calculating poker hands is @Armelline.

    New to GameSalad? (FAQs)   |   Tutorials   |   Templates   |   Greenleaf Games   |   Educator & Certified GameSalad User

  • AppulocityAppulocity Member, PRO Posts: 46

    Ok, sure sure, didn't think it would affect the method but I totally get it

  • tatiangtatiang Member, Sous Chef, PRO, Senior Sous-Chef Posts: 11,949

    Here's the demo I worked on. It uses text strings but could be adapted to numbers. It doesn't check constantly but rather waits for a Touch is Pressed condition. If I were building a poker app, I'd think of a condition for when the rule should trigger. It might be Touch is Pressed within a "deal" button actor or it might be triggered by a game attribute such as a boolean that changes when the player plays a card.

    New to GameSalad? (FAQs)   |   Tutorials   |   Templates   |   Greenleaf Games   |   Educator & Certified GameSalad User

  • AppulocityAppulocity Member, PRO Posts: 46

    @tatiang sorry for the late reply, thanks very much that is incredibly helpful

Sign In or Register to comment.