PDA

View Full Version : Basic Scripting Question


jham77
01-16-2007, 01:32 AM
Dear XSI users.....

I am getting into scripting and sure it speeds up my overall workflow.
However since my scripting level is purely at "copy-paste-edit",
sometimes I am going nowhere but wasting time.


For instance I couldn't figure out the folowing.


Here is what I want to achive with this script
1) setup user input and set it as variable oValue
2) simple math (oValue + 12)
3) print the result

I typed in 12 as my input and if I scripted correctly,
I should get 24 right? (12+12=24)

Yes, VBS is fine but Jscript is returning weird result.
Can anybody please have a look at it and teach me
what I am doing wrong?
[VBS]
oValue = XSIInputBox( "How many?", "?", "0.0" )
z = oValue + 12
LogMessage z

' INFO : 24
[Jscript]
var oValue = XSIInputBox( "How many?", "?", "0.0" );
var z = oValue+12
LogMessage (z)

// INFO : 1212

12+12 = 1212 ???
why...???

misterdi
01-16-2007, 01:49 AM
In JScript there are no difference between number and string.

XSIInputBox return a string -> your oValue treated as string, so your oValue + 12 is read as string concat.

How about forcing it to be number using Number(oValue) which will convert the string into number.

Regards,

TonyEdwards
01-16-2007, 01:54 AM
misterdi beat me...

You can check out w3schools.com for some quick JScript info:
http://www.w3schools.com/js/default.asp

Here's some more info on JScript Type Conversion:
http://www.jibbering.com/faq/faq_notes/type_convert.html

5quid
01-16-2007, 02:02 AM
It's because JScript (and javascript) have a bad habit of treating numbers as string when using the '+' op.

var oValue = XSIInputBox( "How many?", "?", "0.0" );
var tmp = oValue -1 + 1; // force JScript to treat this as a number
var z = tmp + 12
LogMessage (z)



/edit - everyone beat me :)

jham77
01-16-2007, 02:02 AM
Never expected a reply this fast.....
Thank you so much misterdi & TonyEdwards & 5quid
Number(oValue) and var tmp = oValue -l + l;
worked !!!
Thanks again!!!

CGTalk Moderation
01-16-2007, 02:02 AM
This thread has been automatically closed as it remained inactive for 12 months. If you wish to continue the discussion, please create a new thread in the appropriate forum.