Search a multidimensional array
Alex Tweedly
alex at tweedly.net
Fri Apr 20 15:52:32 EDT 2018
On 20/04/2018 20:15, Richard Gaskin via use-livecode wrote:
> This is an interesting problem, Andrew. Thanks for posting it.
>
Indeed!
> One modest performance gain available easily may be to change this:
>
> repeat for each element tThisItem in tInventoryArray
> if tThisItem["description"] contains tSearchQuery then
> put tThisItem into tSortedInventoryArray[ \
> (the number of elements of tSortedInventoryArray) + 1]
> end if
> end repeat
>
> ...to:
>
> put 0 into i
> repeat for each element tThisItem in tInventoryArray
> if tThisItem["description"] contains tSearchQuery then
> add i to i
> put tThisItem into tSortedInventoryArray[i]
> end if
> end repeat
>
I'd go for something like
put tMatchingKeys
repeat for each line K in the keys of tInventoryArray
if tInventoryArray [K]["description"] contains tSearchQuery then
put K & CR after temp
end if
end repeat
( or use repeat for each key K in tInventoryArray
I used that other form because I keep a variable with the set of keys -
and that can be re-used to subsequently search subsets with the 'repeat
for each line' style of repeat :-)
and then use the key list for subsequent access. Unless you are changing
or destroying the 'extracted' elements, it will save you significant
time to just use it in-place.
Also, testing with an earlier version of LC (maybe 7.x ??) showed that
"repeat for each key ..." was faster than "repeat for each element ..."
- no idea why, and haven't retested in at least a couple of years.
I have a library (announced a couple of days ago - don't use that
initial version !!) for doing almost exactly this. For a data table
apparently similar to yours (100 columns, 50,000 rows) this kind of
simple search takes around 180 millisecs (on an aging Macbook Pro).
NB - the latest version is *much* changed from the version on github - I
will finish testing tonight and post the new version to Github and put a
copy on my own website (and announce to this list).
-- Alex.
P.S. I agree with Richard's request for a copy of the data / current
code if it is not sensitive. Feel free to email directly to me if that
is possible.
More information about the use-livecode
mailing list