View Full Version : detecting digits

i8bit

01-16-2014, 06:25 PM

Here's a tough question for you today forums :)

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

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

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.

Starfire2001

01-16-2014, 06:40 PM

Even though you are dealing with an integer substring should work.

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

i8bit

01-16-2014, 07:04 PM

Even though you are dealing with an integer substring should work.

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)?

Starfire2001

01-16-2014, 07:32 PM

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

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.

BlueMelon

01-16-2014, 08:54 PM

Since we are dealing with numbers, I would approach it differently with some simple math.

function numberToArray(temp.n) {

temp.n = abs(temp.n);

temp.digits = {};

while(temp.n != 0) {

temp.digits.insert(0, temp.n % 10);

temp.n = 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(...)

callimuc

01-16-2014, 09:28 PM

Since we are dealing with numbers, I would approach it differently with some simple math.

function numberToArray(temp.n) {

temp.n = abs(temp.n);

temp.digits = {};

while(temp.n != 0) {

temp.digits.insert(0, temp.n % 10);

temp.n = 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

function numberToArray(n) {

temp.n = abs(temp.n); //abs() will always keep your number positive

temp.toReturn = NULL;

for (temp.i=0; temp.i<temp.n.length(); temp.i++) {

temp.toReturn.add(temp.n.substring(temp.i, 1)); //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;

}

BlueMelon

01-16-2014, 09:49 PM

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.

i8bit

01-17-2014, 12:27 PM

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..?

callimuc

01-17-2014, 03:56 PM

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)

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

Torankusu

01-17-2014, 05:59 PM

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...]

//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.n = abs(temp.n);

//setting blank array

temp.digits = {};

//make sure there is a number in temp.n

while(temp.n != 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(0, temp.n % 10);

temp.n = 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...

xAndrewx

01-18-2014, 07:27 AM

this.fullnumber = 482;

this.digitone = this.fullnumber.charat(0);

this.digittwo = this.fullnumber.charat(1);

this.digitthree = this.fullnumber.charat(2);

For a function

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);

}

100Zero100

01-18-2014, 07:59 PM

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()

function numberToArray(num) {

for (temp.i = 0; i < num.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.

xAndrewx

01-19-2014, 09:14 AM

Yeah.

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

https://blogs.oracle.com/greimer/entry/best_way_to_code_a

Not tested on Graal

BlueMelon

01-19-2014, 03:20 PM

If we are talking about 'quickness': you are dealing with integers, treat them as integers.

vBulletin® v3.8.11, Copyright ©2000-2019, vBulletin Solutions Inc.