Legacy GM How to find lowest value of 9 variables

I have nine variables in my code:
The variables are: N1, N2, N3, N4, N5, N6, N7, N8, and N9. These numbers are not an array ..ie not N(1)

What's the most efficient way of finding which of those 9 variables have the lowest value?
 

obscene

Member
Do you want the lowest value, or do you really need to know which variable has the lowest value?

Example, would the result be something like 4 or something like "N7". The lowest value is easy, but the lowest variable name would be trickier.

Edit: This is why an array could help, as you could return 7 instead of "N7".
 
Last edited:
Hmm, what I really need is just the lowest value, not the variable name but I also want to make sure that it doesn't check for 0 value variables, so if N3 = 0 and N5 = 0 then I only want it to find the lowest value from N1, N2, N4, N6, N7, N8 and N9 because N3 and N5 won't apply as they are equal to 0.

Thanks
 

Alexx

Member
I would make a script that adds all values to a dslist and then sorts them.
Then returns the lowest non-zero value (if present).

There are several other ways to approach this.
 

TailBit

Member
As Alexx said..
Code:
var value = smallest(N1,N2,N3,N4,N5,N6,N7,N8,N9);
script named: smallest
Code:
var a;
for(var i=0;i<argument_count;i++) if argument[i]==0 a[i]=9999999 else a[i] = argument[i];
return min(a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7],a[8]);
I think the argument array was read only.. or I would just have used it instead..
 

Yal

🐧 *penguin noises*
GMC Elder
Priority queues are the easiest way to sort data if you want to filter members.

  1. Create a ds_priority
  2. For each potential value, check if it's valid, and if so add it to the priority queue with a priority equal to the value. (I'd usually add an object ID or other refernece as the value, and the evaluation value as the priority, but in your case when you just want the value you can use the value for both)
  3. If the queue isn't empty, just get the max / min value from it and voilà.
  4. Don't forget to destroy the ds_priority afterwards to avoid leaking memory
 

BenRK

Member
Guys... there's a built in function for this...

min(val1,val2,val3,...)

It literally tells you which value is the lowest of a number of given values.
 

Yal

🐧 *penguin noises*
GMC Elder
Guys... there's a built in function for this...

min(val1,val2,val3,...)

It literally tells you which value is the lowest of a number of given values.
Yes, but the OP needs to ignore values that are invalid and min() needs every argument to be inline (no arrays or other variable-size structures).
 

BenRK

Member
Yes, but the OP needs to ignore values that are invalid and min() needs every argument to be inline (no arrays or other variable-size structures).
Gotcha. Then yeah, a simple for loop with arrays will do.
 
Top