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 01-16-2014, 06:25 PM
i8bit i8bit is offline
Registered User
Join Date: Jul 2013
Posts: 146
i8bit is an unknown quantity at this point
detecting digits

Here's a tough question for you today forums

So let's say you have the number, 482 (just as an example)

PHP Code:
this.fullnumber 482
How would I tell the client to detect what number each digit is?
The first digit being 2, the second being 8, and the third being 4.

SO

PHP Code:
this.fullnumber 482;
this.digitone //first digit
this.digittwo //second digit
this.digitthree //third digit 
this.fullnumber will fluctuate so I need the client to detect what number each digit is....If this is even possible.
Reply With Quote
  #2  
Old 01-16-2014, 06:40 PM
Starfire2001 Starfire2001 is offline
Unholy Nation
Starfire2001's Avatar
Join Date: Dec 2010
Location: The streets.
Posts: 156
Starfire2001 will become famous soon enough
Even though you are dealing with an integer substring should work.

PHP Code:
this.fullnumber 482;
this.digitone this.fullnumber.substring(2,1); // 2
this.digittwo this.fullnumber.substring(1,1);  // 8
this.digitthree this.fullnumber.substring(0,1); // 4 
__________________
-Ph8
Reply With Quote
  #3  
Old 01-16-2014, 07:04 PM
i8bit i8bit is offline
Registered User
Join Date: Jul 2013
Posts: 146
i8bit is an unknown quantity at this point
Quote:
Originally Posted by Starfire2001 View Post
Even though you are dealing with an integer substring should work.

PHP Code:
this.fullnumber 482;
this.digitone this.fullnumber.substring(2,1); // 2
this.digittwo this.fullnumber.substring(1,1);  // 8
this.digitthree this.fullnumber.substring(0,1); // 4 
Do you mind explaining the substring a bit more? What is the (2,1) (1,1) and (0,1)?
Reply With Quote
  #4  
Old 01-16-2014, 07:32 PM
Starfire2001 Starfire2001 is offline
Unholy Nation
Starfire2001's Avatar
Join Date: Dec 2010
Location: The streets.
Posts: 156
Starfire2001 will become famous soon enough
Quote:
Originally Posted by i8bit View Post
Do you mind explaining the substring a bit more? What is the (2,1) (1,1) and (0,1)?
From http://gscript.graal.net/substring

Quote:
Syntax: string.substring(index[, length]);
Returns string.

Explanation

Extracts a sub-string starting at index and stopping at the end of the string.
If the optional parameter length is specified, the resulting string will be of the same length and starting at index.
__________________
-Ph8
Reply With Quote
  #5  
Old 01-16-2014, 08:54 PM
BlueMelon BlueMelon is offline
asdfg
BlueMelon's Avatar
Join Date: Sep 2008
Posts: 1,481
BlueMelon is a splendid one to beholdBlueMelon is a splendid one to beholdBlueMelon is a splendid one to beholdBlueMelon is a splendid one to behold
Since we are dealing with numbers, I would approach it differently with some simple math.

PHP Code:
function numberToArray(temp.n) {
  
temp.abs(temp.n);
  
temp.digits = {};
  while(
temp.!= 0) {
    
temp.digits.insert(0temp.10);
    
temp.int(temp.n/10);
  }
  return 
temp.digits;
}

function 
onCreated() {
  
temp.fullnumber 482;

  
temp.digits numberToArray(temp.fullnumber);
  echo(
temp.digits[0]); // 4
  
echo(temp.digits[1]); // 8
  
echo(temp.digits[3]); // 2

If you want it the other way (reversed), change the .insert(0, ...) with .add(...)
__________________
http://i.imgur.com/OOJbW.jpg
Reply With Quote
  #6  
Old 01-16-2014, 09:28 PM
callimuc callimuc is offline
callimuc's Avatar
Join Date: Nov 2010
Location: Germany
Posts: 1,015
callimuc is a splendid one to beholdcallimuc is a splendid one to beholdcallimuc is a splendid one to beholdcallimuc is a splendid one to beholdcallimuc is a splendid one to behold
Quote:
Originally Posted by BlueMelon View Post
Since we are dealing with numbers, I would approach it differently with some simple math.

PHP Code:
function numberToArray(temp.n) {
  
temp.abs(temp.n);
  
temp.digits = {};
  while(
temp.!= 0) {
    
temp.digits.insert(0temp.10);
    
temp.int(temp.n/10);
  }
  return 
temp.digits;
}

function 
onCreated() {
  
temp.fullnumber 482;

  
temp.digits numberToArray(temp.fullnumber);
  echo(
temp.digits[0]); // 4
  
echo(temp.digits[1]); // 8
  
echo(temp.digits[3]); // 2

If you want it the other way (reversed), change the .insert(0, ...) with .add(...)
Probably would have done something like
PHP Code:
function numberToArray(n) {
  
temp.abs(temp.n); //abs() will always keep your number positive
  
temp.toReturn NULL;
  for (
temp.i=0temp.i<temp.n.length(); temp.i++) {
    
temp.toReturn.add(temp.n.substring(temp.i1)); //first add 4, then 8, then 2 (in your example);
    /*
    If you would like to have the 2 first, then 8 and then 4 (numbers taken from your example again), then just replace
    temp.toReturn.add(temp.n.substring(temp.i, 1));
    with
    temp.toReturn.insert(0, temp.n.substring(temp.i, 1));
    */
  
}
  return 
temp.toReturn;

__________________
MEEP!
Reply With Quote
  #7  
Old 01-16-2014, 09:49 PM
BlueMelon BlueMelon is offline
asdfg
BlueMelon's Avatar
Join Date: Sep 2008
Posts: 1,481
BlueMelon is a splendid one to beholdBlueMelon is a splendid one to beholdBlueMelon is a splendid one to beholdBlueMelon is a splendid one to behold
From my tests, the method I posted is faster then using substring. But I'm going to assume for OP's case it doesn't really mater which is more efficient.
__________________
http://i.imgur.com/OOJbW.jpg
Reply With Quote
  #8  
Old 01-17-2014, 12:27 PM
i8bit i8bit is offline
Registered User
Join Date: Jul 2013
Posts: 146
i8bit is an unknown quantity at this point
Not saying you guys are wrong, but for me, Starfire's script is the only one I can really understand. Till new to scripting..

I'm still confused on what Return is doing in script..?
Reply With Quote
  #9  
Old 01-17-2014, 03:56 PM
callimuc callimuc is offline
callimuc's Avatar
Join Date: Nov 2010
Location: Germany
Posts: 1,015
callimuc is a splendid one to beholdcallimuc is a splendid one to beholdcallimuc is a splendid one to beholdcallimuc is a splendid one to beholdcallimuc is a splendid one to behold
Quote:
Originally Posted by i8bit View Post
Not saying you guys are wrong, but for me, Starfire's script is the only one I can really understand. Till new to scripting..

I'm still confused on what Return is doing in script..?
his one would just work at its best if you know/send the length of your numbers, while the one BlueMelon and me posted could be used as a function used by several scripts (like putting it into a class with custom functions)

Quote:
Originally Posted by BlueMelon View Post
From my tests, the method I posted is faster then using substring. But I'm going to assume for OP's case it doesn't really mater which is more efficient.
Interesting to know
__________________
MEEP!
Reply With Quote
  #10  
Old 01-17-2014, 05:59 PM
Torankusu Torankusu is offline
Elite Member
Torankusu's Avatar
Join Date: Jun 2001
Posts: 10,065
Torankusu has a spectacular aura aboutTorankusu has a spectacular aura about
Quote:
Originally Posted by i8bit View Post
Not saying you guys are wrong, but for me, Starfire's script is the only one I can really understand. Till new to scripting..

I'm still confused on what Return is doing in script..?
Here you go [BlueMelon's method...]

PHP Code:
//This is a function for breaking down a large
//number and storing it as individual elements in an array
function numberToArray(temp.n) {
  
//defining temp.n as an absolute value -- no negatives
  
temp.abs(temp.n);
  
//setting blank array
  
temp.digits = {};
  
  
//make sure there is a number in temp.n
  
while(temp.!= 0) {
    
//if you don't know what % does,
    //look up "modulo" on google
    //this is inserting each digit into the
    //temp.digits array from earlier
    
temp.digits.insert(0temp.10);
    
temp.int(temp.n/10);
  }
  
//this returns the array we just set up...
  
return temp.digits;
}

function 
onCreated() {
  
temp.fullnumber 482;
  
  
//here's an example of what return does...
  //explained below...
  
temp.digits numberToArray(temp.fullnumber);
  echo(
temp.digits[0]); // 4
  
echo(temp.digits[1]); // 8
  
echo(temp.digits[2]); // 2

Return basically works like this:
you can set up a function to perform some actions, like sorting these numbers 54925 into an array. well once you've sorted them and put them into the array, you are telling the function to RETURN the data you've gathered or sorted when that function is called

in the example above temp.digits = The Array the function numberToArray(temp.fullnumber) is returning...

Hope that helps a bit...
__________________
Quote:
Originally posted by Spark910
Think befreo you type.
Reply With Quote
  #11  
Old 01-18-2014, 07:27 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
HTML Code:
this.fullnumber = 482; 
this.digitone = this.fullnumber.charat(0);
this.digittwo = this.fullnumber.charat(1);
this.digitthree = this.fullnumber.charat(2);
For a function

HTML Code:
function onCreated() {
  this.number = 2663;
  echo( this.getNumbersAsArray(this.number) ); //returns {2, 6, 6, 3}
  echo( this.getNumber(2, this.number)); //returns 6
}

function getNumbersAsArray(number) {
  temp.ret = {};
  for (temp.i = temp.number.length(); temp.i > 0; temp.i--;)
    temp.ret.insert(0, temp.number.charat(temp.i - 1));
  return temp.ret;
}

function getNumber(index, number) {
  return temp.number.charat(temp.index);
}
__________________
Reply With Quote
  #12  
Old 01-18-2014, 07:59 PM
100Zero100 100Zero100 is offline
Registered User
Join Date: Jul 2006
Posts: 31
100Zero100 is on a distinguished road
Quote:
Originally Posted by callimuc View Post
his one would just work at its best if you know/send the length of your numbers, while the one BlueMelon and me posted could be used as a function used by several scripts (like putting it into a class with custom functions)



Interesting to know
Starfire's method works with substring() too, for multiple numbers, by using temp.num.length() looping.

I disagree with everybody. I would use .charat()

PHP Code:
function numberToArray(num) {
 for (
temp.0num.length(); i++) {
  
temp.array.add(num.charat(i)); // like num.substring(i, 1) but more efficient
 
}
 return(array);

So
temp.num = 426;
temp.test = numberToArray(num);

temp.test is {4, 2, 6}

You could also use charat() to most efficiently get the number at a single position, eg num.charat(1) would be 2.

EDIT: Someone beat me to charat. Darn. But looping backwards and using .insert is the same as using .add and looping forwards, Andrew.

EDIT2: I wonder if concatenation/tokenize would be more efficient than .add()? If so, temp.array = array SPC num.charat[i]; + tokenize it on the return.
__________________
Hi. I'm NaS!

Last edited by 100Zero100; 01-18-2014 at 08:30 PM..
Reply With Quote
  #13  
Old 01-19-2014, 09:14 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
Yeah.

Well, the reason I used the backwards loop is because it's always quicker- well in JavaScript anyway

https://blogs.oracle.com/greimer/ent..._way_to_code_a

Not tested on Graal
__________________
Reply With Quote
  #14  
Old 01-19-2014, 03:20 PM
BlueMelon BlueMelon is offline
asdfg
BlueMelon's Avatar
Join Date: Sep 2008
Posts: 1,481
BlueMelon is a splendid one to beholdBlueMelon is a splendid one to beholdBlueMelon is a splendid one to beholdBlueMelon is a splendid one to behold
If we are talking about 'quickness': you are dealing with integers, treat them as integers.
__________________
http://i.imgur.com/OOJbW.jpg
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:50 PM.


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