Hi,
Coming from 25+ years of assembler, C, C++, Java, Javascript, Python...+ functional programming...etc...I am starting to like LiveCode.
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.
How does one convert a String to an integer in LiveCode?
Moderators: FourthWorld, heatherlaine, Klaus, robinmiller
-
- 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?
Add 0 to tString
(0=Zero)
Marek
(0=Zero)
Marek
-
- 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?
Hi Camster,
Livecode does not have types. Basically everything is treated as string and turned into a integer when asked to.
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
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
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?
Wow, that's it? Talking about a paradigm shift! Thank you Marek and Bernd for your help.
Camster.
Camster.