Search for a string in an array?
#1

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?
Reply
#2

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
    }
}
Reply
#3

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
    }
}
Reply
#4

Ah, right. Sorry 'bout that.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)