2

How can I search an array to find out whether it contains a set of values within the specific range of elements in the array (may be from position 2 to position 7, the first elements would be start of frame, length etc )

if input_data[1:5] == (45, 35, 230, 28)

if contains 45 
    send sensor 1 data;
if contains 35
    send sensor 2 data;
if contains 230 
    send sensor 3 data;
if contains 28
    send sensor 4 data;

It may contain any combinations (order) of the above values or doesn't contain any one of the elements and should stop sending data if the associated element is not found

I am trying to send some requested data based on the received elements

Updated !!

The data is received only once(only when updated), according to the Received data i need to send the requested outgoing data in the background,

if input_data[1:5] == (45, 35, 230, 28)
if contains 45 
    send_Sensordata1 = 1

if contains 28
    send_Sensordata4 = 1

in other function (running in background)

if send_Sensordata1 ==1 
   do something here(main sending stuff goes here)
if send_Sensordata4 ==1 
   do something here (main sending stuff goes here)

so in the first incoming data i get all the elements, then the requested data is sent, but if an element is missing in the next incoming data i need to stop sending outgoing data for that particular request (like toggle)

6
  • loop and if statements perhaps? Commented Dec 17, 2015 at 4:10
  • ...but if an element is missing in the next incoming data.... Does the array itself gets updated when the next incoming data arrives? Commented Dec 17, 2015 at 5:43
  • @ Haris .. yes it gets updated, so if i get the incoming data as (45,35,230,28) it wants all the data from all the sensors. Similarly when new incomming data is (45,230) i need to send outgoing data only to those requests and stop the remaining Commented Dec 17, 2015 at 5:50
  • Then the code I posted will work, the only thing missing is that you want it to be done in the background. Can you explain a little more about that, do you want it to be done in parallel? Commented Dec 17, 2015 at 6:05
  • How do your array looping code and the code which will send data in the background communicate? Do they run in different threads or processes? Commented Dec 17, 2015 at 6:30

2 Answers 2

3

You have to loop over the array and look for the those particular elements every time.

for(i=2; i<7; i++)
{
    if(array[i] == 45) 
        send sensor 1 data;
    else if(array[i] == 35)
        send sensor 2 data;
    else if(array[i] == 230) 
        send sensor 3 data;
    else if(array[i] == 28)
        send sensor 4 data;
}
Sign up to request clarification or add additional context in comments.

1 Comment

@ Haris , i was able to run it, but got into other issue, have a look at the updated question
0

The array loop will start from 1 to get element 2 as in c array start from 0.

for(i=1; i<7; i++)
{
    if(array[i] == 45) 
        send sensor 1 data;
    else if(array[i] == 35)
        send sensor 2 data;
    else if(array[i] == 230) 
        send sensor 3 data;
    else if(array[i] == 28)
        send sensor 4 data;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.