checking for a integer?
|
|
David Tengu
Registered User
Join date: 20 Sep 2006
Posts: 11
|
10-24-2006 07:29
I'm building a vendor script. basic plan is so far is to use a notecard to control the stock. each line of the note card will be like this itemname|price IE Buy me|500 now what i want to do is check the price part to make sure its an integer. I've already build the script to strip down the notecard line to the text before and after the | so the price is temp stored in STRafter I just want to make sure that the data in STRafter is an integer. is there a command i can use i.e. if (isinteger(STRafter)) llsay(0,"STRafter is a number  "  ; Thanks in advance 
_____________________
Welcome, David Tengu. You last visited: 12-31-1969 at 11:00 PM
|
|
Eloise Pasteur
Curious Individual
Join date: 14 Jul 2004
Posts: 1,952
|
10-24-2006 07:40
As long as you don't sell freebies, if((integer)STRafter>0) llOwnerSay("STRafter is an integer"  ; It doesn't quite do it, something that start with a digit, e.g. 4 elephants will detect as a number, but it's close enough for most purposes in a vendor.
|
|
David Tengu
Registered User
Join date: 20 Sep 2006
Posts: 11
|
10-24-2006 07:45
done a bit of playing and i've found this list test = ["string", 0];
if (llGetListEntryType(test, 0) == 1) llSay(0,"0 Is an int"); else if (llGetListEntryType(test, 0) != 1) llSay(0,"0 Isn't an int"); if (llGetListEntryType(test, 1) == 1) llSay(0,"1 Is an int"); else if (llGetListEntryType(test, 1) != 1) llSay(0,"1 Isn't an int");
which gives the output: 0 Isn't an int 1 Is an int EDIT: Scrap that.... will need to look into it more thanks Eloise Pasteur - I'll keep it in mind
_____________________
Welcome, David Tengu. You last visited: 12-31-1969 at 11:00 PM
|
|
Newgate Ludd
Out of Chesse Error
Join date: 8 Apr 2005
Posts: 2,103
|
10-24-2006 08:10
Eloise's solution works unless someone types a O or o instead of a zero, i..e 4oo would give 4 not 400!
You could iterate each character and check for it being a numeric.
|
|
David Tengu
Registered User
Join date: 20 Sep 2006
Posts: 11
|
10-24-2006 08:37
From: Newgate Ludd You could iterate each character and check for it being a numeric. i've been playing around with (integer) to check if its a integer. looks like i will have to do a for loop and check each char, but if i use the (integer) you first have to make sure that the char is not "0" first so its going to be something like this get length of string grab first char check letter to see it it equals "0" if "0" then OK and move onto the next char set tempint to (integer)first letter if tempint > 0 the OK and move onto next char grab next char repeat until end of string Blahh, stuff it for now. atm I'm the only one who will be using the vendor. I'll just make sure i'm not being a idiot while i'm writing the notecard and worry about checking this later on, spent far too much time trying to figure this out when all i wanted to do was write a vendor script
_____________________
Welcome, David Tengu. You last visited: 12-31-1969 at 11:00 PM
|
|
Newgate Ludd
Out of Chesse Error
Join date: 8 Apr 2005
Posts: 2,103
|
10-25-2006 02:19
From: David Tengu i've been playing around with (integer) to check if its a integer. looks like i will have to do a for loop and check each char, but if i use the (integer) you first have to make sure that the char is not "0" first
so its going to be something like this
get length of string grab first char check letter to see it it equals "0" if "0" then OK and move onto the next char set tempint to (integer)first letter if tempint > 0 the OK and move onto next char grab next char repeat until end of string
Dont you mean check in range "0" - "9" ? string characters = "0123456789";
integer IsNumeric(string a) { integer len = llStringLength(a); integer result = FALSE; if(len != 0) { result = TRUE; integer index = 0; do { string chara = llGetSubString(a,index,index); integer posa = llSubStringIndex(characters,chara); if((posa < 0) { result = FALSE; index = len; } ++index; } while(index < len); } return result; }
|
|
David Tengu
Registered User
Join date: 20 Sep 2006
Posts: 11
|
10-25-2006 09:52
From: Newgate Ludd Dont you mean check in range "0" - "9" ?
well what was going thou my head at the time was using (integer) to check, but soon thought that woul be a waste of time... read your code, thats extactly what i wanted to do. Cheers  I was writing something along those lines, thanks for pointing me in the right direction. its a shame its not a function in LSL itself. thanks again 
_____________________
Welcome, David Tengu. You last visited: 12-31-1969 at 11:00 PM
|
|
Newgate Ludd
Out of Chesse Error
Join date: 8 Apr 2005
Posts: 2,103
|
10-26-2006 01:00
From: David Tengu well what was going thou my head at the time was using (integer) to check, but soon thought that woul be a waste of time... read your code, thats extactly what i wanted to do. Cheers  I was writing something along those lines, thanks for pointing me in the right direction. its a shame its not a function in LSL itself. thanks again  Yw. I also have IsAlpha and IsAlphanumeric functions which strangely enough work on very similar lines. In fact the version i posted was a bastardised version explicitly for Numbers. I have a few string functions that I have put together over the past year when I have needed them, strcmp, strncmp etc. Most have been posted in the forums. I will try to add them to the Wikki / Script Library. UPDATEString Functions now added to the Script Library
|