# Searching for minimum value in an array?

Get help using Construct 2

### » Fri Jul 17, 2015 4:34 pm

The code I have looks something like:

Array row : [uid1, uid2, uid3, int]

Each of these rows with this data arrangement is set up when a laser line hits a "target". The three uids of the objects involved are stored (firer, laser, target), but the thing I'm really looking to do is compare the int at the end of the array.

Say I have an array data setup loaded like this with three rows.
[101, 201, 301, 500]
[101, 201, 302, 750]
[101, 201, 303, 1000]

In this setup I have the same firer for all three rows, the same laser beam, but three different targets at three different distances.

How can I search across the array to find the minimum distance in the fourth Y value, in a way that doesn't require knowledge of the number of rows I am searching across?
B
4
Posts: 7
Reputation: 214

### » Fri Jul 17, 2015 5:05 pm

just a thought .. example.capx
B
67
S
24
G
7
Posts: 1,519
Reputation: 11,084

### » Fri Jul 17, 2015 5:05 pm

The number of rows is irrelevant as you will always have to search the entire array. The easiest method of doing it is a function with two local variables, that will return the index of the row with the minimum int. First, set the localVar1 to array.At(0,3) and localVar2 to 0, then Repeat array.width-1 -> if array.at(loopindex,3) < localVar1 then set localVar1 to array(loopindex,3) and set localVar2 to loopindex. After the loop, set return value to localVar2. Then you can access the array at the correct index and do whatever you want like Array.At(Function.Call("functionName"), [any y index]) to get a particular uid or the value.
B
80
S
33
G
27
Posts: 1,027
Reputation: 21,114