Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

AppGameKit Classic Chat / struggling with searching a string array

Author
Message
Zen Jones
6
Years of Service
User Offline
Joined: 1st Jul 2017
Location:
Posted: 4th Jun 2018 06:07
array sample


search code


Before anyone asks, ND, N1 N2 etc indicate NOTE D, NOTE 1, NOTE 2 etc... there are entries in the array that are just "1" "2" "3" etc..

There's something WRONG here, and I can't find it. It works IF I'm searching for "ND", "N1", "N2", "N3", or "N4". However the moment I add another entry, it spits it back and says not there. also ND sometimes works, and sometimes doesn't. It's incredibly frustrating to have a problem that doesn't appear to be a problem anywhere, especially a bug that is intermittent and random with the same code.
Zen Jones
6
Years of Service
User Offline
Joined: 1st Jul 2017
Location:
Posted: 4th Jun 2018 08:27

By replacing line 18 with the following 4 lines, my code works fine. Clearly more than one way to skin a cat, and .find() can't really find very much.
IronManhood
9
Years of Service
User Offline
Joined: 6th Feb 2015
Location: US
Posted: 4th Jun 2018 09:09 Edited at: 4th Jun 2018 09:10
Quote: "Arrays can now be sorted using the .sort() command and searched using the .find(item) command. For example .sort() will always sort in ascending order and .find(item) will only work on arrays that are in ascending order. If .find(item) cannot find the item you are looking for it will return "-1". You can sort arrays of Integers, Float, Strings, and Types. When sorting types the first variable of the type will be used to compare elements."


I'm not sure if this is your problem.
Bengismo
6
Years of Service
User Offline
Joined: 20th Nov 2017
Location: Yorkshire, England
Posted: 4th Jun 2018 09:41
I was just about to post the same thing. That array isnt sorted so the .find() may not work

You could just sort the array or loop through the array entries and compare the array string with the search string and that will do the find your expecting.
Zen Jones
6
Years of Service
User Offline
Joined: 1st Jul 2017
Location:
Posted: 4th Jun 2018 10:31
The problem is simply that rearranging the items in 'order' would break the whole point of having the array to begin with. First I have no idea what 'order' it would decide, but I know when it got done, the frame number reference would no longer be index+1.
Phaelax
DBPro Master
21
Years of Service
User Offline
Joined: 16th Apr 2003
Location: Metropia
Posted: 4th Jun 2018 13:50
I'm guessing this is an array of notes arranged to make a song and this is why you cannot sort the array? If that's the case, then a sequential search really is the only way to find the element you want. You could write a variation of a binary search. Worst case scenario would be a search time of O(n) but a possibility of O(0.5n), plus a slight overhead of additional FOR loops.

In your existing FOR loop, you should EXIT the loop once you found the index you want.
Tiled TMX Importer V.2
XML Parser V.2
Base64 Encoder/Decoder
Purple Token - Free online hi-score database
Legend of Zelda

"I like offending people, because I think people who get offended should be offended." - Linus Torvalds
Zen Jones
6
Years of Service
User Offline
Joined: 1st Jul 2017
Location:
Posted: 4th Jun 2018 14:00
it's a picture map for sprites, noting where each sprite is in relation to the index, which allows for very simple image swaps with PlaySprite() The first table is correlating frame numbers for the second table which is where the images are on the grid.

Login to post a reply

Server time is: 2024-04-26 03:23:26
Your offset time is: 2024-04-26 03:23:26