Graal Forums  

Go Back   Graal Forums > Development Forums > NPC Scripting
FAQ Members List Calendar Today's Posts

Reply
 
Thread Tools Search this Thread Display Modes
  #1  
Old 12-16-2007, 12:38 AM
Googi Googi is offline
A Serious Epidemic
Googi's Avatar
Join Date: Oct 2001
Location: Canada
Posts: 18,866
Googi has much to be proud ofGoogi has much to be proud ofGoogi has much to be proud ofGoogi has much to be proud ofGoogi has much to be proud ofGoogi has much to be proud of
Send a message via AIM to Googi
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.
__________________
Reply With Quote
  #2  
Old 12-16-2007, 01:52 AM
xAndrewx xAndrewx is offline
Registered User
xAndrewx's Avatar
Join Date: Sep 2004
Posts: 5,260
xAndrewx has much to be proud ofxAndrewx has much to be proud ofxAndrewx has much to be proud ofxAndrewx has much to be proud ofxAndrewx has much to be proud ofxAndrewx has much to be proud ofxAndrewx has much to be proud of
PHP Code:
function onCreated() {
  
temp.= {4357};
  
temp.f.sortascending();

  echo(
temp.f[0]);

You even said how to do it, he he
__________________
Reply With Quote
  #3  
Old 12-16-2007, 01:59 AM
Kyranki Kyranki is offline
Freelance Coder
Join Date: Aug 2007
Location: At the end of the rainbow, in the pot of gold.
Posts: 202
Kyranki is on a distinguished road
Send a message via AIM to Kyranki Send a message via MSN to Kyranki
Googie wanted a simpler way to do it besides using sort ascending dear Andrew.
__________________
Stan.
Reply With Quote
  #4  
Old 12-16-2007, 02:06 AM
DustyPorViva DustyPorViva is offline
Will work for food. Maybe
DustyPorViva's Avatar
Join Date: Sep 2003
Location: Maryland, USA
Posts: 9,589
DustyPorViva has a reputation beyond reputeDustyPorViva has a reputation beyond reputeDustyPorViva has a reputation beyond reputeDustyPorViva has a reputation beyond reputeDustyPorViva has a reputation beyond reputeDustyPorViva has a reputation beyond reputeDustyPorViva has a reputation beyond reputeDustyPorViva has a reputation beyond reputeDustyPorViva has a reputation beyond reputeDustyPorViva has a reputation beyond reputeDustyPorViva has a reputation beyond repute
Send a message via AIM to DustyPorViva Send a message via MSN to DustyPorViva
PHP Code:
temp.lowest=999999;
for (
i:temp.array) {
  if (
i<temp.lowesttemp.lowest=i;

temp.lowest=lowest number
This what you looking for?
Reply With Quote
  #5  
Old 12-16-2007, 02:12 AM
Darklux Darklux is offline
Petrification of a Newbie
Darklux's Avatar
Join Date: Dec 2002
Location: Dortmund, Germany
Posts: 1,375
Darklux is a jewel in the roughDarklux is a jewel in the rough
Send a message via ICQ to Darklux Send a message via AIM to Darklux Send a message via Yahoo to Darklux
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.
Reply With Quote
  #6  
Old 12-16-2007, 02:15 AM
DustyPorViva DustyPorViva is offline
Will work for food. Maybe
DustyPorViva's Avatar
Join Date: Sep 2003
Location: Maryland, USA
Posts: 9,589
DustyPorViva has a reputation beyond reputeDustyPorViva has a reputation beyond reputeDustyPorViva has a reputation beyond reputeDustyPorViva has a reputation beyond reputeDustyPorViva has a reputation beyond reputeDustyPorViva has a reputation beyond reputeDustyPorViva has a reputation beyond reputeDustyPorViva has a reputation beyond reputeDustyPorViva has a reputation beyond reputeDustyPorViva has a reputation beyond reputeDustyPorViva has a reputation beyond repute
Send a message via AIM to DustyPorViva Send a message via MSN to DustyPorViva
I guess there's always:
PHP Code:
temp.numbers={5,1,7,8,2};
temp.lowest=temp.numbers.sortascending()[0]; 
As well.
Reply With Quote
  #7  
Old 12-16-2007, 02:19 AM
Googi Googi is offline
A Serious Epidemic
Googi's Avatar
Join Date: Oct 2001
Location: Canada
Posts: 18,866
Googi has much to be proud ofGoogi has much to be proud ofGoogi has much to be proud ofGoogi has much to be proud ofGoogi has much to be proud ofGoogi has much to be proud of
Send a message via AIM to Googi
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.
__________________
Reply With Quote
  #8  
Old 12-16-2007, 02:23 AM
Kyranki Kyranki is offline
Freelance Coder
Join Date: Aug 2007
Location: At the end of the rainbow, in the pot of gold.
Posts: 202
Kyranki is on a distinguished road
Send a message via AIM to Kyranki Send a message via MSN to Kyranki
Quote:
Originally Posted by DustyPorViva View Post
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.
__________________
Stan.
Reply With Quote
  #9  
Old 12-16-2007, 02:24 AM
Googi Googi is offline
A Serious Epidemic
Googi's Avatar
Join Date: Oct 2001
Location: Canada
Posts: 18,866
Googi has much to be proud ofGoogi has much to be proud ofGoogi has much to be proud ofGoogi has much to be proud ofGoogi has much to be proud ofGoogi has much to be proud of
Send a message via AIM to Googi
Quote:
Originally Posted by Kyranki View Post
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.
__________________
Reply With Quote
  #10  
Old 12-16-2007, 02:35 AM
DustyPorViva DustyPorViva is offline
Will work for food. Maybe
DustyPorViva's Avatar
Join Date: Sep 2003
Location: Maryland, USA
Posts: 9,589
DustyPorViva has a reputation beyond reputeDustyPorViva has a reputation beyond reputeDustyPorViva has a reputation beyond reputeDustyPorViva has a reputation beyond reputeDustyPorViva has a reputation beyond reputeDustyPorViva has a reputation beyond reputeDustyPorViva has a reputation beyond reputeDustyPorViva has a reputation beyond reputeDustyPorViva has a reputation beyond reputeDustyPorViva has a reputation beyond reputeDustyPorViva has a reputation beyond repute
Send a message via AIM to DustyPorViva Send a message via MSN to DustyPorViva
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.
Reply With Quote
  #11  
Old 12-16-2007, 05:26 AM
cbk1994 cbk1994 is offline
the fake one
cbk1994's Avatar
Join Date: Mar 2003
Location: San Francisco
Posts: 10,718
cbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond repute
Send a message via AIM to cbk1994
Quote:
Originally Posted by Googi View Post
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
__________________
Reply With Quote
  #12  
Old 12-16-2007, 05:52 AM
Googi Googi is offline
A Serious Epidemic
Googi's Avatar
Join Date: Oct 2001
Location: Canada
Posts: 18,866
Googi has much to be proud ofGoogi has much to be proud ofGoogi has much to be proud ofGoogi has much to be proud ofGoogi has much to be proud ofGoogi has much to be proud of
Send a message via AIM to Googi
Quote:
Originally Posted by cbkbud View Post
HOW2SCRIPT
You're so cool.
__________________
Reply With Quote
  #13  
Old 12-16-2007, 06:29 AM
Novo Novo is offline
[TServerDeveloper]
Join Date: Jun 2006
Posts: 448
Novo will become famous soon enough
Well...

PHP Code:
temp.lowest temp.arr.sortascending()[0];
temp.indexOfLowest temp.arr.indexOftemp.lowest ); 
?!
Reply With Quote
  #14  
Old 12-16-2007, 07:41 AM
DustyPorViva DustyPorViva is offline
Will work for food. Maybe
DustyPorViva's Avatar
Join Date: Sep 2003
Location: Maryland, USA
Posts: 9,589
DustyPorViva has a reputation beyond reputeDustyPorViva has a reputation beyond reputeDustyPorViva has a reputation beyond reputeDustyPorViva has a reputation beyond reputeDustyPorViva has a reputation beyond reputeDustyPorViva has a reputation beyond reputeDustyPorViva has a reputation beyond reputeDustyPorViva has a reputation beyond reputeDustyPorViva has a reputation beyond reputeDustyPorViva has a reputation beyond reputeDustyPorViva has a reputation beyond repute
Send a message via AIM to DustyPorViva Send a message via MSN to DustyPorViva
Owned
Reply With Quote
  #15  
Old 12-16-2007, 10:13 PM
Googi Googi is offline
A Serious Epidemic
Googi's Avatar
Join Date: Oct 2001
Location: Canada
Posts: 18,866
Googi has much to be proud ofGoogi has much to be proud ofGoogi has much to be proud ofGoogi has much to be proud ofGoogi has much to be proud ofGoogi has much to be proud of
Send a message via AIM to Googi
Quote:
Originally Posted by Novo View Post
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); 
__________________
Reply With Quote
  #16  
Old 12-16-2007, 10:46 PM
DustyPorViva DustyPorViva is offline
Will work for food. Maybe
DustyPorViva's Avatar
Join Date: Sep 2003
Location: Maryland, USA
Posts: 9,589
DustyPorViva has a reputation beyond reputeDustyPorViva has a reputation beyond reputeDustyPorViva has a reputation beyond reputeDustyPorViva has a reputation beyond reputeDustyPorViva has a reputation beyond reputeDustyPorViva has a reputation beyond reputeDustyPorViva has a reputation beyond reputeDustyPorViva has a reputation beyond reputeDustyPorViva has a reputation beyond reputeDustyPorViva has a reputation beyond reputeDustyPorViva has a reputation beyond repute
Send a message via AIM to DustyPorViva Send a message via MSN to DustyPorViva
He sorted it, but did so in another variable, so the original array is untouched.
Reply With Quote
  #17  
Old 12-17-2007, 12:05 AM
Googi Googi is offline
A Serious Epidemic
Googi's Avatar
Join Date: Oct 2001
Location: Canada
Posts: 18,866
Googi has much to be proud ofGoogi has much to be proud ofGoogi has much to be proud ofGoogi has much to be proud ofGoogi has much to be proud ofGoogi has much to be proud of
Send a message via AIM to Googi
Quote:
Originally Posted by DustyPorViva View Post
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).
__________________
Reply With Quote
  #18  
Old 12-17-2007, 03:25 AM
napo_p2p napo_p2p is offline
oh snaps
napo_p2p's Avatar
Join Date: Sep 2003
Location: Pismo Beach, California
Posts: 2,118
napo_p2p has a spectacular aura aboutnapo_p2p has a spectacular aura about
Send a message via AIM to napo_p2p Send a message via MSN to napo_p2p
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.
__________________
Scito hoc super omnia.
Haec vita est tua una sola.
Dum vita superest, utere maxime quoque puncto, momento, et hora quae habes.
Tempus neminem non manet.
Noli manere tempus.
Carpe Diem

Seize the Day.
Reply With Quote
  #19  
Old 12-18-2007, 03:59 AM
cbk1994 cbk1994 is offline
the fake one
cbk1994's Avatar
Join Date: Mar 2003
Location: San Francisco
Posts: 10,718
cbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond repute
Send a message via AIM to cbk1994
Quote:
Originally Posted by Googi View Post
You're so cool.
Thank you, ma'am.
__________________
Reply With Quote
Reply


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +2. The time now is 07:11 PM.


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