Searching within an array

Hello,

String platters [] = {"34ABC01", "34ABC02", "34ABC03", "34ABC04", "34ABC05"};

I have an array of type string, and I want to do a search with a variable of type string with indexof or some other method in it, and how to code to be able to produce a search result,

(Message variable has string data)

int status;
status = platters.indexOf (message);
if (status == -1)
{digitalWrite(13,HIGH);}
....

I used the codes but I get the following error:

Request for member 'indexOf' in 'platters', which is of non-class type 'String [5]'

How can I solve this problem?

Use a loop to compare your message against each element of platters.

It is not a good idea to use the String (capital S) class on an Arduino as it can cause memory corruption in the small memory on an Arduino. Just use cstrings - char arrays terminated with 0.

...R

shn79:
Hello,

String platters [] = {"34ABC01", "34ABC02", "34ABC03", "34ABC04", "34ABC05"};

I have an array of type string, and I want to do a search with a variable of type string with indexof or some other method in it, and how to code to be able to produce a search result,

I wouldn't use String at all. I would do something like this (demo code you can compile and try):

int main (void)
{
    char buffer[128]; // just a buffer to print to
    const char *to_find = "34ABC03"; // string we want to find (comes from somewhere else in real life)
    const char *platters[] = { // strings to search through
        "34ABC01",
        "34ABC02",
        "34ABC03",
        "34ABC04",
        "34ABC05",
    };
    int len = (sizeof (platters) / sizeof (*platters)); // how many elements in array
    int x; // generic loop counter

    init(); // arduino setup
    Serial.begin (115200);

    for (x = 0; x < len; x++) {
        sprintf (buffer, "Looking for %s (at index %d)...", to_find, x);
        Serial.print (buffer);
        if (strcmp (to_find, platters[x]) == 0) { // this is the key: a match returns 0
            sprintf (buffer, "Found %s in array at index %d!\r\n", platters[x], x);
            Serial.print (buffer);
        } else {
            sprintf (buffer, "Fail. %s is not %s at index %d!\r\n", to_find, platters[x], x);
            Serial.print (buffer);
        }
    }

    while (1); // done, nuthin else to do but hang out
}

The program should return text on the serial port like this:

[b]Looking for 34ABC03 (at index 0)...Fail. 34ABC03 is not 34ABC01 at index 0!
Looking for 34ABC03 (at index 1)...Fail. 34ABC03 is not 34ABC02 at index 1!
Looking for 34ABC03 (at index 2)...Found 34ABC03 in array at index 2!
Looking for 34ABC03 (at index 3)...Fail. 34ABC03 is not 34ABC04 at index 3!
Looking for 34ABC03 (at index 4)...Fail. 34ABC03 is not 34ABC05 at index 4![/b]

Hope this helps.

(by the way, those "strings" look like hexadecimal numbers to me. IF they are, you can use "strtoul" instead to convert them to unsigned long integers and then simply compare them numerically rather than as strings).

Thanks, i will try the solutions :slight_smile:

shn79:
Thanks, i will try the solutions :slight_smile:

You are welcomed. And it's "solution", since both Robin2 and I said basically the same thing (I just was a bit more elaborate about it).

String platters [] = {"34ABC01", "34ABC02", "34ABC03", "34ABC04", "34ABC05"};That's a really good point about the hex numbers.
The array " platters" consumes 30 bytes of RAM before you have even taken into account the string data, another 40 bytes.
Assuming the values are simple hex, you're using 70 bytes of precious RAM to store 20 bytes of information.

Hello, the problem I mentioned is resolved as below, thank you guys :slight_smile:

Const char * plate = "34AKM01"; // data to be retrieved from the outside

Const char * dplakalar [] =
{
"34AKM01",
"34AKM02",
"34AKM03"
}

for (x = 0; x <len; x ++)
{
if (strcmp (plate, dplakalar [ x ]) == 0 )
{
Gecerli_Plaka ();
break;
}

I have string data in the message, converting rf to verbose individual string as follows. But how do I equalize a char * plate with a string message? So I want to equalize the data in the message when the plate data is not fixed, should I convert the string back to char, or should I transfer the incoming data to the plate without any joining?

Uint8_t buf [VW_MAX_MESSAGE_LEN];
Uint8_t buflen = VW_MAX_MESSAGE_LEN;

if (vw_get_message (buf, and buflen))
{
for (i; i < buflen; i++)
{
c = char ( buf [ i ] );
s = string (c);
message + = s;

}

}

// const char * plate = "34AKM01"; // data to be retrieved from the outside
This line * plate = message; I want to do it

Hello, I delivered the project and got the degree, thank you to the helpers, arduino forum, as good as sharing :slight_smile:

Good to hear. Thanks for the feedback.

When can we expect the beer?

...R