Page 1 of 1

How does one convert a String to an integer in LiveCode?

Posted: Thu Aug 22, 2013 12:55 am
by Camster
Hi,

Coming from 25+ years of assembler, C, C++, Java, Javascript, Python...+ functional programming...etc...I am starting to like LiveCode. :D
Question:
How does one convert a String to an integer in LiveCode? (like "parseInt()" in java and JavaScript...or "strtonum" in C, C++ )

Thank you.

Re: How does one convert a String to an integer in LiveCode?

Posted: Thu Aug 22, 2013 6:11 am
by snm
Add 0 to tString
(0=Zero)
Marek

Re: How does one convert a String to an integer in LiveCode?

Posted: Thu Aug 22, 2013 7:32 am
by bn
Hi Camster,

Livecode does not have types. Basically everything is treated as string and turned into a integer when asked to.

Code: Select all

on mouseUp
   put "these 5 brave people" into field 1 -- obviously a string
   wait 1000 milliseconds -- let's see it
   add 1 to word 2 of field 1 -- word 2 is treated as integer inside a string
   wait 1000 milliseconds
   try
      add 1 to word 1 of field 1 -- this will not work since word 1 is not an integer
   catch tErr
      put cr & tErr after field 1 -- see the error
   end try
end mouseUp
since it is not possible to add 1 to word 1 (these) this will throw an error
Jacque has made a stack to look up error codes:
http://revonline2.runrev.com/stack/712/ ... ror-Lookup

So you don't have to declare types, just use them implicitly. Probably this takes a bit getting used to but will feel natural after a short while.

There are subtleties in string number conversions that are hidden in everyday use
for a discussion
http://www.sonsothunder.com/devres/live ... crp007.htm
So Marek has a point in his post. But this is behind the scene and you are not forced to add 0 to a variable to make it an integer. It will increase execution speed a bit in special situations though.


Kind regards
Bernd

Re: How does one convert a String to an integer in LiveCode?

Posted: Fri Aug 23, 2013 3:14 am
by Camster
Wow, that's it? Talking about a paradigm shift! :) Thank you Marek and Bernd for your help.

Camster.