Followers 0

# array in studio5000

## 6 posts in this topic

Hi All,

I have two DINT array for example array1 [200] = [ 0, 1.........199], array2[100] = [ 20,40,60 ...180]

array1[30] = 30 need to compare it with array2[100]  and find the closest range.

in my example array1[30] is between array2[0] and array2[1] how can I do that in studio5000?

I haven't worked with array before and therefore your support is appreciated

Thanks

##### Share on other sites

I can think of some brute force ways to do it with loops, depending on what you want to do with the outcomes.

Are the values in the arrays changing dynamically? Are the values in Array2 always in ascending order? What do you want to do with the information once you have it? In other words, do you need to continuously step through all 200 values of array 1 to see where they fit in array 2?

##### Share on other sites

well, the value in array 1 changes dynamically, but in array 2 is not. and its ascending order.

I want to check the array 1 element value with array 2 values and get the closest bigger  number in array 2 so I can update my target to the closest number  and repeat that for each element in array 1

example:

target position =0

current array[1] = 20000,

compare with the constant array 2 , which is between 18000 and 25000 values

then take the closest bigger number and move it to the target position

##### Share on other sites

Not a huge fan of this, but it seems to work to look up a single array element. This may not work well for rapidly changing values.

The LIM instruction makes sure the value to be looked up is within the range of values stored in the array.

I think I like this method better:

Both methods take about 100 PLC scans to search the array. If you need it to update in one PLC scan, you'll need to use a JMP/LBL pair:

You will need either one rung per element in array1 or add an outer loop to index through the array1 elements in a similar way that this loops through the array2 elements.

##### Share on other sites

Thanks a lot,

That was very helpful, I did it same way and it works as I wanted.

Thanks again

##### Share on other sites

I suggest using the FSC command.

expression: array1[x] LEQ array2[FSC_controltag.pos]

use the result to reference the particular value from array2