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

Want to move your code and projects to LiveCode but don't know where to start?

Moderators: FourthWorld, heatherlaine, Klaus, robinmiller

Post Reply
Camster
Posts: 7
Joined: Tue Aug 06, 2013 6:51 pm
Location: New York, USA

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

Post by Camster » Thu Aug 22, 2013 12:55 am

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.

snm
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 253
Joined: Fri Dec 09, 2011 11:17 am
Location: Warszawa / Poland

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

Post by snm » Thu Aug 22, 2013 6:11 am

Add 0 to tString
(0=Zero)
Marek

bn
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3975
Joined: Sun Jan 07, 2007 9:12 pm
Location: Bochum, Germany

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

Post by bn » Thu Aug 22, 2013 7:32 am

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

Camster
Posts: 7
Joined: Tue Aug 06, 2013 6:51 pm
Location: New York, USA

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

Post by Camster » Fri Aug 23, 2013 3:14 am

Wow, that's it? Talking about a paradigm shift! :) Thank you Marek and Bernd for your help.

Camster.

Post Reply

Return to “Converting to LiveCode”