PDA

View Full Version : Determining the smallest of a group of variables.

Googi
12-16-2007, 12:38 AM
Say I have a bunch of variables and want to figure out which one is the smallest. Obviously I can just load them into an array and do sortbyascending then take the first value and see which of the variables it matches, but I was wondering if there's a less convoluted way.

xAndrewx
12-16-2007, 01:52 AM
function onCreated() {
temp.f = {4, 3, 5, 7};
temp.f.sortascending();

echo(temp.f[0]);
}

You even said how to do it, he he

Kyranki
12-16-2007, 01:59 AM
Googie wanted a simpler way to do it besides using sort ascending dear Andrew.

DustyPorViva
12-16-2007, 02:06 AM
temp.lowest=999999;
for (i:temp.array) {
if (i<temp.lowest) temp.lowest=i;
}
temp.lowest=lowest number
This what you looking for?

Darklux
12-16-2007, 02:12 AM
I suggest mergesort or quicksort,
the effort of it is just rising by n log n for n as the natural size of the problem.

DustyPorViva
12-16-2007, 02:15 AM
I guess there's always:
temp.numbers={5,1,7,8,2};
temp.lowest=temp.numbers.sortascending()[0];
As well.

Googi
12-16-2007, 02:19 AM
The thing is, I want to get the variable name, not its value. What I'm looking for is a simpler way of getting the variable name than getting the lowest value and then checking each variable if it matches the value, not a simpler way of getting the lowest value.

Kyranki
12-16-2007, 02:23 AM
temp.lowest=999999;
for (i:temp.array) {
if (i<temp.lowest) temp.lowest=i;
}
temp.lowest=lowest number
This what you looking for?

Then this is what you're looking for.

Googi
12-16-2007, 02:24 AM
Then this is what you're looking for.

That just returns the value of the variable with the lowest value, not the name of the variable. I'd still have to check every variable to see if it matches that value.

EDIT: Came up with a way to do it:

temp.lowest=999999;
temp.arrayvalue = 0;
for (i:temp.array) {
if (i<temp.lowest) {
temp.lowest = i;
temp.lowestposition = temp.arrayvalue;
}
temp.arrayvalue++;
}

Gives the lowest value and its position in the array, which is pretty much just as good since each variable's array position is known.

DustyPorViva
12-16-2007, 02:35 AM
Ah, I completely misread, I thought you said numbers in an array, not a set of variables. In that case:

temp.lowest={-1,999999};
for (i=0;i<temp.array.size();i++) {
if (i<temp.lowest) {
temp.lowest={i,temp.array[i]};
}
}
It's a little more compact.

cbk1994
12-16-2007, 05:26 AM
That just returns the value of the variable with the lowest value, not the name of the variable. I'd still have to check every variable to see if it matches that value.

EDIT: Came up with a way to do it:

temp.lowest=999999;
temp.arrayvalue = 0;
for (i:temp.array) {
if (i<temp.lowest) {
temp.lowest = i;
temp.lowestposition = temp.arrayvalue;
}
temp.arrayvalue++;
}

Gives the lowest value and its position in the array, which is pretty much just as good since each variable's array position is known.

HOW2SCRIPT

Googi
12-16-2007, 05:52 AM
HOW2SCRIPT

You're so cool.

Novo
12-16-2007, 06:29 AM
Well...

temp.lowest = temp.arr.sortascending()[0];
temp.indexOfLowest = temp.arr.indexOf( temp.lowest );

?!

DustyPorViva
12-16-2007, 07:41 AM
Owned :(

Googi
12-16-2007, 10:13 PM
Well...

temp.lowest = temp.arr.sortascending()[0];
temp.indexOfLowest = temp.arr.indexOf( temp.lowest );

?!

Wouldn't that just set temp.indexOfLowest to 0 because you've sorted temp.arr to be in ascending order? Although you could do:

temp.sortarr = temp.arr;
temp.lowest = temp.sortarr.sortascending()[0];
temp.indexOfLowest = temp.arr.indexOf(temp.lowest);

DustyPorViva
12-16-2007, 10:46 PM
He sorted it, but did so in another variable, so the original array is untouched.

Googi
12-17-2007, 12:05 AM
He sorted it, but did so in another variable, so the original array is untouched.

I thought that since sortascending is a function, it changes the array regardless of whether it's being used within a variable assignment or on its own line (at least, this is the way it is in PHP).

napo_p2p
12-17-2007, 03:25 AM
sortascending() just sorts the array. It does not even return a sorted array. Something like:
temp.lowest = temp.arr.sortascending()[0];
Would just set temp.lowest to null.

I guess you could make a sortascending2 and do:
function sortascending2(arr) {
temp.result = temp.arr;
temp.result.sortascending();
return temp.result;
}

Then:
temp.lowest = sortascending2(temp.arr)[0];
temp.indexOfLowest = temp.arr.index( temp.lowest );

Should work.

cbk1994
12-18-2007, 03:59 AM
You're so cool.

Thank you, ma'am.