Graal Forums

Graal Forums (https://forums.graalonline.com/forums/index.php)
-   NPC Scripting (https://forums.graalonline.com/forums/forumdisplay.php?f=8)
-   -   Determining the smallest of a group of variables. (https://forums.graalonline.com/forums/showthread.php?t=77940)

Googi 12-16-2007 12:38 AM

Determining the smallest of a group of variables.
 
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

PHP Code:

function onCreated() {
  
temp.= {4357};
  
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

PHP Code:

temp.lowest=999999;
for (
i:temp.array) {
  if (
i<temp.lowesttemp.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:
PHP Code:

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

Quote:

Originally Posted by DustyPorViva (Post 1364030)
PHP Code:

temp.lowest=999999;
for (
i:temp.array) {
  if (
i<temp.lowesttemp.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

Quote:

Originally Posted by Kyranki (Post 1364035)
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:

PHP Code:

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:
PHP Code:

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

Quote:

Originally Posted by Googi (Post 1364036)
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:

PHP Code:

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

Quote:

Originally Posted by cbkbud (Post 1364049)
HOW2SCRIPT

You're so cool.

Novo 12-16-2007 06:29 AM

Well...

PHP Code:

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

?!

DustyPorViva 12-16-2007 07:41 AM

Owned :(

Googi 12-16-2007 10:13 PM

Quote:

Originally Posted by Novo (Post 1364056)
Well...

PHP Code:

temp.lowest temp.arr.sortascending()[0];
temp.indexOfLowest temp.arr.indexOftemp.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:

PHP Code:

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

Quote:

Originally Posted by DustyPorViva (Post 1364124)
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:
PHP Code:

temp.lowest temp.arr.sortascending()[0]; 

Would just set temp.lowest to null.

I guess you could make a sortascending2 and do:
PHP Code:

function sortascending2(arr) {
  
temp.result temp.arr;
  
temp.result.sortascending();
  return 
temp.result;


Then:
PHP Code:

  temp.lowest sortascending2(temp.arr)[0];
  
temp.indexOfLowest temp.arr.indextemp.lowest ); 

Should work.

cbk1994 12-18-2007 03:59 AM

Quote:

Originally Posted by Googi (Post 1364054)
You're so cool.

Thank you, ma'am.


All times are GMT +2. The time now is 08:47 AM.

Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2026, vBulletin Solutions Inc.
Copyright (C) 1998-2019 Toonslab All Rights Reserved.