ArraySearch - Forum

Forum Navigation
You need to log in to create posts and topics.

ArraySearch

Hi All

I would like to know if it is possible to use ArraySearch with an Object, unless my syntax is bad, because  when I try there is a crash :

"Uncaught TypeError: str is undefined" or "Uncaught TypeError: a is undefined"

example mydata : id, name, surname, age

ArraySearch [mydata('name')] "Kate" [position] or ArraySearch [mydata.name] "Kate" [position]

@phil78 you are using objects as if they were Arrays? Note that they are different things.
This will work:

CreateEmptyObject [mydata]
SetVar [mydata.name] "My name is Kate Holmes"
SearchStr "Kate" "[mydata.name]" [position]

Regards.

I do not have any result with SearchStr. My object is mydata from example neoTableTutorialPart1.

As we use Arraylen  to know the number of entries of the object, that's why I thought to use ArraySearch

@phil78 note that you are using an Array of Objects. Just use a loop to iterate all the Array items and use SearchStr to find those that match your string search. If I find time I would try to attach an example later.

Regards.

Hi again @phil78, please take a look at the attached sample.
What I have done:

Create alll the data in Project > Events:

CreateEmptyArray [data]
Loop 0 5 [n]
   CreateEmptyObject [data([n])]
EndLoop
SetVar [data(0).name] "Peter"
SetVar [data(1).name] "Ursula"
SetVar [data(2).name] "Sandra"
SetVar [data(3).name] "John"
SetVar [data(4).name] "Carmen"
SetVar [data(5).name] "Lara"

SetVar [data(0).surname] "Andrews"
SetVar [data(1).surname] "Clark"
SetVar [data(2).surname] "Strongarm"
SetVar [data(3).surname] "Carpenter"
SetVar [data(4).surname] "Miranda"
SetVar [data(5).surname] "Conrado"

Add a TextInput field whose Variable is [textToSearch]

Add a Container to show [name] and [surname]

Add a button to fire the search functionality:

SetVar [name] ""
SetVar [surname] ""
ArrayLen [data] [total]
Loop 0 [total] [n]
  StrSearch "[textToSearch]" "[data([n]).name]" [position]
  If [position] > -1
     SetVar [name] [data([n]).name]
     SetVar [surname] [data([n]).surname]
     ExitLoop
  EndIf
EndLoop

It's important to understand that we are searching within a text string (name), that is field of an Object, that is an element in a Array. I hope it helps :)

Regards.

Uploaded files:
  • You need to login to have access to uploads.
Ronin has reacted to this post.
Ronin

@luishp Thank you again Luis. I was thinking there was a predefined function. It is perfect for me.

with your example I have add this expression because array start with 0

Math "[total]-1" 0 [total]

Best regards

Hello everyone, I would like to know if, using this example that Luis presented, is there a code to continue with the search, that is, to place the "find next" function on the button (for the rest of the matches)?
THANK YOU

@iosoft take a look at the main loop:

Loop 0 [total] [n]

It searches from the first array element 0 to the last one [total] and stores the current position being searched on [n]

When you find an occurrence, just before the ExitLoop, store [n] plus 1 in a new variable (for instance [s])

SetVar [s] [n]+1

So you can start next search in [n]+1 position, that's the next one to the last match occurrence:

Loop [s] [total] [n]

Just remember to initialize [s] to 0 the first time you use this new loop.

Regards.

 

 

Vadim has reacted to this post.
Vadim

Spectacular! Now I was able to specify what I was looking for.
Thank you so much
Cheers

luishp has reacted to this post.
luishp