How do I find index of text match in an array?

  • Hi,

    I am storing user data in an array and I am separating each entry with a "|||" separator. I can load these fine but I have given each individual an id number that they enter on another website. The data gets pulled down using an ajax request URL - this all works fine. What I want to do is search through the array and find the occurrence of the users id number in that array and then get the index of that occurrence so as to only load that persons data.

    So the question is how do I find the index of an occurrence of string in an array?

    Luke

  • You can do this:

    Array For each element

    ...Array Compare current value -> Set index to Array.CurX

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • Thanks dop2000

    I've seen that elsewhere.

    What does that look like in the Event sheet?

    Cheers

    Luke

  • Not sure what you mean..

    Add event: Array For each element

    Add sub-event: Array Compare current value ... (your search string)

    Add action: Set (variable) to Array.CurX

  • I take it that the string can't be a part of the string.

  • If you need to find a part of the string in array, instead of "Array compare current value" condition use this:

    System -> Compare two values -> find(Array.CurValue, searchString) greater or equal 0

  • Thanks but I still don't see how I am getting the index of where C2 finds the string in the array

  • See my second comment.

    When you are using "Array for each element", Array.CurX will contain the index of current element in the arry.

  • Hi,

    I am storing user data in an array and I am separating each entry with a "|||" separator. I can load these fine but I have given each individual an id number that they enter on another website. The data gets pulled down using an ajax request URL - this all works fine. What I want to do is search through the array and find the occurrence of the users id number in that array and then get the index of that occurrence so as to only load that persons data.

    So the question is how do I find the index of an occurrence of string in an array?

    Luke

    Maybe this is what you mean.

    If I have a number in an array to find, and i want to find the index of that I use the following, I normally store the result in a global variable.

    Array > contains value str(User ID here) -------- System set Global Variable to indexof(str(User ID))

    you need str, if you are finding a number, and not a string, otherwise str is not needed.

    Hope this might be what you are looking for?

    Cheers

  • F&N Brilliant!

    Thanks sizcoz

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)