SA-MP Forums Archive
Search for a string in an array? - Printable Version

+- SA-MP Forums Archive (https://sampforum.blast.hk)
+-- Forum: SA-MP Scripting and Plugins (https://sampforum.blast.hk/forumdisplay.php?fid=8)
+--- Forum: Scripting Help (https://sampforum.blast.hk/forumdisplay.php?fid=12)
+--- Thread: Search for a string in an array? (/showthread.php?tid=431363)



Search for a string in an array? - Programie - 17.04.2013

Hi,

what is the most efficient way of searching for a string in an array?

For example I have an array holding all vehicle models and if I search for "Box" it should find 3 elements (498 - Boxville, 590 - Freight Box, 609 - Boxville).

Currently I have all my vehicle models and object models in a MySQL database and simply use the following SQL to search for the model:

Код:
SELECT `id` FROM `vehiclemodels` WHERE `name` LIKE = '%Box%';
But I decided to not save static, never changing stuff like vehicle or object models in the database and instead place them directly in the code as an array.

I want create commands which also accept only a part of the vehicle name or object name to be created:

/v inf
/addobject tree


Any ideas?


Re: Search for a string in an array? - Scenario - 17.04.2013

Something like this, perhaps?

pawn Код:
for(new i = 0; i < sizeof(array); i++)
{
    if(strfind(array[i], "box"))
    {
        // it was found; the position in the array is: i
    }
}



Re: Search for a string in an array? - Programie - 17.04.2013

Oh right there is strfind.

Thanks.


I've searched for strfind on the SA-MP Wiki: The function returns -1 if the string was not found.

So the code should be:

Код:
for(new i = 0; i < sizeof(array); i++)
{
    if(strfind(array[i], "box") != -1)
    {
        // it was found; the position in the array is: i
    }
}



Re: Search for a string in an array? - Scenario - 17.04.2013

Ah, right. Sorry 'bout that.