Here's the updated function :) It should now handle all negative numbers and other anomalies correctly
conststringnum="0123456789";//Strips the blank space paddings from either end of a stringstringStrip(strings){intbegin=1;intend=StringLength(s);//Return if no blank spacesif(StringFind(s," ",false)==-1){returns;}//Find startwhile(StringSub(s,begin,begin)==" "){begin+=1;}//Find endwhile(StringSub(s,end,end)==" "){end-=1;}returnStringSub(s,begin,end);}//Checks if the the first non-space character is a numberboolStrIsNum(strings,boolisFixed){inti=1;intlen=StringLength(num);intpos;stringx=Strip(s);stringsub;//Check if isNaNif(x==""||x=="."||x=="-."||x==".-"){returnfalse;}//Check if negativepos=StringFind(x,"-",false);if(pos==1){x=StringReplaceWord(x,"-","",1,false);}elseif(pos!=-1){returnfalse;}//Check if intwhile(i<=len){x=StringReplaceWord(x,StringSub(num,i,i),"",c_stringReplaceAll,false);i+=1;}//Check if floating pointif(isFixed){x=StringReplaceWord(x,".","",1,false);}//If string length 0 after replacing all numbers, it's a numberreturnStringLength(x)==0;}intStr2Int(strings){if(StrIsNum(s,false)){returnStringToInt(s);}return0;}fixedStr2Fix(strings){if(StrIsNum(s,true)){returnStringToFixed(s);}return0;}
Attached my test file as well. Feel free to critique if you feel the function could be further optimized (Esp the strip)
Lol, thanks for the feedback. I'll weed those out when I'm free :) (Probably on the weekend)
However, I believe ".123" is a valid float number. The majority of programming languages recognize it as so (Not sure about Galaxy though). Does it throw errors with StringToFixed?
Note to self "-.", ".-", " -1" may cause complications as well. Have to be very careful with the replacement of blank spaces as "1 0" actually throws an error. Much further testing required. @_@
For floating point numbers you should avoid replacing all "." since this would allow stuff like "12.34.25", instead id suggest just to replace the first .
I noticed this last night when I was trying to sleep lol. Fixed it above. As for negative numbers (which I forgot), with some modifications, I believe it can be built to handle negative numbers (Check if the first character is a '-') Will give it a try when I'm able.
No error, but When I tried another script I was working on, it spammed my debugger with the (value: ) could not be assigned nonsense. Trying to figure out here what exactly the condition is for the error to appear.
Edit:
I found the condition. The debugger will only spam if the StringToInt function is called from a trigger thread. Doesn't matter how many stacks deep. If you're running from the main thread (InitMain), nothing will happen.
Edit #2:
Here's the solution I've come up with, partially inspired by your solution above :)
conststringnum="0123456789";//Checks if the the first non-space character is a numberboolStrIsNum(strings,boolisFixed){stringx=s;intlen=StringLength(num);inti=1;while(i<=len){x=StringReplaceWord(x,StringSub(num,i,i),"",c_stringReplaceAll,false);i+=1;}//Check if floating pointif(isFixed){x=StringReplaceWord(x,".","",1,false);}//If string length 0 after replacing all numbers, it's a numberreturnStringLength(x)==0;}intStr2Int(strings){if(StrIsNum(s,false)){returnStringToInt(s);}return0;}fixedStr2Fix(strings){if(StrIsNum(s,true)){returnStringToFixed(s);}return0;}
I'm not sure how it'll perform with super long strings though.
Could I take a look at your test map please? I don't understand why I'm not getting the error, although you still get it. I'd like to do a comparison between our methods
Did they ninja-fix the StringToInt problem? I was trying to find a better algorithm for your post, but I can't seem to replicate the problem. I faced it too before, but now it's not spitting errors (I'm not complaining).
Note: cout is just a debugger function which prints to screen. i2s is a shortcut for IntToString.
The output I got when I run this was: 0, 0, 1, 1, 0
Edit:
The StringToInt function appears to behave in a manner that it will check for the first non-whitespace character in the string. If it's an integer, it will still cast, else it will return 0. Here's what I came up with to check this condition. Although it won't really matter if they fixed it
@Mille25: Go
Here's the updated function :) It should now handle all negative numbers and other anomalies correctly
Attached my test file as well. Feel free to critique if you feel the function could be further optimized (Esp the strip)
@Mille25: Go
Lol, thanks for the feedback. I'll weed those out when I'm free :) (Probably on the weekend)
However, I believe ".123" is a valid float number. The majority of programming languages recognize it as so (Not sure about Galaxy though). Does it throw errors with StringToFixed?
Note to self "-.", ".-", " -1" may cause complications as well. Have to be very careful with the replacement of blank spaces as "1 0" actually throws an error. Much further testing required. @_@
You're welcome
I suppose I could run some time tests for this one to evaluate it's performance. Do you think a 1000 char string would suffice?
I noticed this last night when I was trying to sleep lol. Fixed it above. As for negative numbers (which I forgot), with some modifications, I believe it can be built to handle negative numbers (Check if the first character is a '-') Will give it a try when I'm able.
@Mille25: Go
Hm.. I tried your script, and here's my output
No error, but When I tried another script I was working on, it spammed my debugger with the (value: ) could not be assigned nonsense. Trying to figure out here what exactly the condition is for the error to appear.
Edit: I found the condition. The debugger will only spam if the StringToInt function is called from a trigger thread. Doesn't matter how many stacks deep. If you're running from the main thread (InitMain), nothing will happen.
Edit #2:
Here's the solution I've come up with, partially inspired by your solution above :)
I'm not sure how it'll perform with super long strings though.
Could I take a look at your test map please? I don't understand why I'm not getting the error, although you still get it. I'd like to do a comparison between our methods
Did they ninja-fix the StringToInt problem? I was trying to find a better algorithm for your post, but I can't seem to replicate the problem. I faced it too before, but now it's not spitting errors (I'm not complaining).
Here's my script:
Note: cout is just a debugger function which prints to screen. i2s is a shortcut for IntToString.
The output I got when I run this was: 0, 0, 1, 1, 0
Edit:
The StringToInt function appears to behave in a manner that it will check for the first non-whitespace character in the string. If it's an integer, it will still cast, else it will return 0. Here's what I came up with to check this condition. Although it won't really matter if they fixed it