C++ version of in_array

In php I would do something like this :

$fruit = array("apple", "orange", "banana", "pear");
if (in_array("pear", $fruit)) {
.... do something....;}

I have searched for the C++ equivalent and it gets confusing with long discussions.

Could someone please help with a simple bit of efficient code that will work with the Arduino IDE.

Thanks in advance.

It depends, to a certain extent, on the representation of the data.

AWOL:
It depends, to a certain extent, on the representation of the data.

What data?
I want to create an array.
If a value is in that array, then the sketch will do something, otherwise it will do nothing.

    if ((sensor != 5592405) && (sensor != 14102864)) {
      Serial.print("Unknown Sensor ");
      Serial.println(sensor);
      }

The above is the code I am using now with only 2 values.
I will have several more values.
I want to simplify the code by using an array function rather than a series of "!="s.

I can also live with the converse i.e. "==" as opposed to "!=".

So now we've gone from strings to numbers?

a simple bit of efficient code

Simple is not always efficient, and vice versa.

@AWOL : If u are not going to be helpful, why bother responding.
As a moderator I would have expected better than ridicule from you.

What data?
I want to create an array.

The data in the array.
Determining whether a string is in an array is different from finding whether a number is in an array, hence AWOL's comment.

Where's the ridicule?
Seriously, point me to it.

I'm trying to get answers that will help me and others help you.

Surely you can see that data representation has a crucial bearing on your solution?

aisc:
@AWOL : If u are not going to be helpful...

I find his two posts to be quite helpful. I suggest rereading them. I will give you a hint: PHP is not a strongly typed language.

As a moderator I would have expected better than ridicule from you.

I suggest you demand your money be returned post haste.

I think one issue here is there is a presumption of a certain knowledge level of C/C++.

The only programming language I know which remotely resembles C/C++ is PHP.

In PHP the only difference between using a string or a number is to enclose the string in quotes viz.

$fruit = array("apple", "orange", "banana", "pear");
if (in_array("pear", $fruit)) {
.... do something....;}

$int = array(111, 222, 333, 444);
if (in_array(333, $int)) {
.... do something....;}

AWOL:
It depends, to a certain extent, on the representation of the data.

Sorry but to my reading this is an obscurely phrased question - and not very clear.
Nonetheless I sought clarification and I responded with my actual code.

AWOL:
So now we've gone from strings to numbers?

This is what I regard as ridicule since it offers no help and is in no way constructive.

UKHeliBob:
The data in the array.
Determining whether a string is in an array is different from finding whether a number is in an array, hence AWOL's comment.

This post by UKHeliBob by comparison is a clear and helpful post.
It tells me I have to unthink my PHP thinking.

AWOL:
Where's the ridicule?
Seriously, point me to it.

I'm trying to get answers that will help me and others help you.

Surely you can see that data representation has a crucial bearing on your solution?

I am not sure why u were still looking for answers when I gave the actual code I wanted to replace...
FWIW the use of the term "data representation" does not impart clear meaning. Data type would have more specific meaning to me. And no, your assumption is not correct - I did not realise there was a crucial difference.

I have re-read them several times, and whilst they may be helpful to someone with your level of expertise, I am not at your level.

We can sit here all day arguing semantics, but that's helping no-one.

Is your list (for want of a better word) of "known" devices known at compile time?

Is it held in RAM, EEPROM, SD card, FIFO, punched cards, notches on a stick . .?

Is it ordered in any way? Datatype may be relevant, but representation in a structure may be even more important.

Is it to be amended (added to, deleted from) during the lifetime of the sketch?

Are there any hard time constraints?

BTW, the word "you" is spelled "you".

AWOL:
We can sit here all day arguing semantics, but that's helping no-one.

I agree and appreciate your help.

Is your list (for want of a better word) of "known" devices known at compile time?

Yes.

Is it held in RAM, EEPROM, SD card, FIFO, punched cards, notches on a stick . .?

Initially it will be part of the sketch, so I suppose in ram.

At a later point I would like to have it on and SD card and call it up from there.
The idea being the SD card can be edited without the sketch being edited, but I fear I won't have enough memory for the SDCard library.

As an alternative to the SD card I would explore having it is EEPROM - but that is totally new territory for me.

Is it ordered in any way? Datatype may be relevant, but representation in a structure may be even more important.

The list will be manually compiled, so can be ordered ascending or descending.
Data type would be long intergers.

Is it to be amended (added to, deleted from) during the lifetime of the sketch?

Yes quite possibly, hence the desire to have it on an SD card.

BTW, the word "you" is spelled "you".

And would you believe I also teach English?

OK, you remember that game where one of you thinks of a number between 1 and 100, and the other has to guess it, and the first can only say if the guess is higher or lower or same?
The best strategy (apart from waterboarding) finds the number in a time proportional to log2 of the size of the list.
It's called a binary search or binary chop, but the data must remain ordered (but need not be contiguous).
Otherwise a simple stroll through the data may be all it takes.

aisc:
@AWOL : If u are not going to be helpful, why bother responding.
As a moderator I would have expected better than ridicule from you.

AWOL is trying to help. OP has come in here waving is hands, with no clear description of a problem, with no code (with data types), and seeming no inclination to provide any details or code.

You are the one that is out-of-line.

@AWOL : I did actually respond earlier, or so I thought, but my post obviously did not make it to the board. From memory this is what I wrote earlier...

I think what you are saying (in layman's) terms is find the length of the array, step through the array and compare each element of the array to the value I am checking for.

I know and understand that concept and could write that code in PHP. My problem is I am not able to adapt it for the Arduino IDE.

The C++ searches I did have not proved much help because they introduce a whole slew of new (to me) terms and inclusions in the code examples. For now I guess I am just going to have to leave this in the too hard basket.

@PaulS : What exactly is the objective of your post? - which incidently is also inaccurate in terms of content. I suggest you re-read the entire thread. You will note AWOL and I have cleared the air and put our differences aside and decided to move forward. Why you would want to come in after the fact to try to muddy the waters again is beyond me.

Much like beauty is in the eye of the beholder, only the recipient of any advice really knows if he/she has actually been helped.

Although I may have learnt something, the fact that I am giving up on this issue should provide a clear indication as to whether I believe I have been helped or not.

As I intimated in an earlier post, not everybody has the same level of C/C++ expertise whilst some only need a clue, others like myself need a bit more of a spoon feeding.

I may be off the mark here, but sifting through all your prose I think this is what you're after. It is by no means efficient, you'll have to sort your data and traverse it in a binary tree method for efficiency:

long data[] = 
{
	234,
	544,
	873,
	// etc ...
};

int num_elements = sizeof( data )/sizeof( long );
int check_value = 455;
bool found = false;

for( int i=0; i<num_elements; i++ )
{
	if( data[i] == check_value )
	{
		// in the list ! ...
		found = true;
	}
}

if( !found )
{
	// not in the list ...
}

Obviously makes no sense to have all those vars local, but you'll be able to sort that issue.

bsearch() is the choice for sorted data.

And to go with bsearch, qsort() to sort it.

@aisc: When two posters have 32K and 57K posts with 600+/1000+ karma points, poking the bears with a pointy stick is probably not a good idea. The two posters have earned the right to demand cogent, well-stated problems and they took you to task because you didn't follow the guidelines established for this Forum, as stated in Nick Gammon's two posts at the top of this Forum. That said, I'm glad that fences are (being) mended and that you're throwing the pointy stick away...good idea.

tammytam:
I may be off the mark here, but sifting through all your prose I think this is what you're after. It is by no means efficient, you'll have to sort your data and traverse it in a binary tree method for efficiency:

long data[] = 

{
234,
544,
873,
// etc ...
};

int num_elements = sizeof( data )/sizeof( long );
int check_value = 455;
bool found = false;

for( int i=0; i<num_elements; i++ )
{
if( data[i] == check_value )
{
// in the list ! ...
found = true;
}
}

if( !found )
{
// not in the list ...
}




Obviously makes no sense to have all those vars local, but you'll be able to sort that issue.

@tammytam : You are correct, that is basically what I was looking for.

In any case I solved my problem by using a range with the < & > comparators.

aisc:
@AWOL : If u are not going to be helpful, why bother responding.
As a moderator I would have expected better than ridicule from you.

Welcome to the world of compiled languages. Unfortunately, he is being helpful because he's being consistent with the first statement. It depends on the nature of the data. Perl and PHP (which was based on perl) are interpreted languages and are considerably 'dumbed down' allowing for poor type casting (which can create it's own share of headaches). And mind you, this is coming from a guy who mostly programs in Perl and PHP (and makes my money off people who don't understand things such as this nuance btw)

Like it or not, lower level languages (which speak more closely to how computers actually work) need firmer definitions of the data they are handling. So either you need far more elaborate routines to detect and thus 'switch' their behavior to handle the data in question (which reduces efficiency and increases the size of your code, memory usage, processing time, etc - a BAD thing on embedded systems which are limited in all of the above) or you have to write your routines specific to the types of data structures you are going to be using.

i.e. if you have an array of integers, you write a function to detect an element in an array of integers specifically. For an array of string, you write one to detect a specific string and so on.

SW