Using a number as a string vs numeral

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller

Post Reply
sritcp
Posts: 431
Joined: Tue Jun 05, 2012 5:38 pm

Using a number as a string vs numeral

Post by sritcp » Fri Feb 22, 2013 4:26 pm

I have a 10x10 matrix where each cell is a graphic.
I would like to name them by their respective serial number: 1, 2, 3, ...., 100.
I'm afraid that this would create a problem, since LiveCode automatically converts the string form of a number to its numerical value.
Does

Code: Select all

set the backgroundColor of graphic "23" to blue
refer to the cell named "23" or to the 23rd graphic on the card (which may not be cell "23")?

Thanks,
Sri.

Klaus
Posts: 14199
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: Using a number as a string vs numeral

Post by Klaus » Fri Feb 22, 2013 4:32 pm

Namaste Sri,
sritcp wrote:...Does

Code: Select all

set the backgroundColor of graphic "23" to blue
refer to the cell named "23" or to the 23rd graphic on the card (which may not be cell "23")?
Yes, the engine does not like this 8)

Simply add a prefix like any character from a-z -> "m1" (maybe m for matrix, or whatever...)
which can easily be stripped off in a script.


Best

Klaus

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10331
Joined: Wed May 06, 2009 2:28 pm

Re: Using a number as a string vs numeral

Post by dunbarx » Fri Feb 22, 2013 4:42 pm

Hi.

What Klaus means is that you should never name anything with a pure number. There are too many ways that the engine can become confused. So if you have a field, say, named "15", it is almost a certainty that you will try to put something into "field 15", and then what is LC supposed to make of it?

AS Klaus suggested, change your thinking to "a1". "a2", etc.

Craig Newman

paul_gr
Posts: 319
Joined: Fri Dec 08, 2006 7:38 pm

Re: Using a number as a string vs numeral

Post by paul_gr » Fri Feb 22, 2013 7:23 pm

Hi Sri,
Using a number as a name is something you don't do in any programming language, not just Livecode.

Paul

sritcp
Posts: 431
Joined: Tue Jun 05, 2012 5:38 pm

Re: Using a number as a string vs numeral

Post by sritcp » Fri Feb 22, 2013 8:59 pm

Yes, I get it!

Thanks,
Sri.

P.S. Namaste to you, Klaus!

Simon
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3901
Joined: Sat Mar 24, 2007 2:54 am

Re: Using a number as a string vs numeral

Post by Simon » Fri Feb 22, 2013 11:12 pm

What about using the graphics ID number?
If they were laid out sequentially then they are easy to address.

Code: Select all

on mouseUp
   put 0 into S
   put 0 into T
   repeat with Q = 1 to 10
   repeat with N = 0 to 9
      create graphic "g"&Q&N
      set the rect of graphic ("g"&Q&N) to S,T,S +50,T+50
      add 50 to S
   end repeat
   add 50 to T
   put 0 into S
    end repeat
end mouseUp
There is a 10x10 grid of 50px x 50px squares with sequential ID numbers and almost sequential names(will need to change 10 of them, I'm too lazy). If you wanted a different size then just change all the 50's to a new number.

Simon EDIT: Fixed the code so that the names are also in sequential order...too easy
I used to be a newbie but then I learned how to spell teh correctly and now I'm a noob!

Randy Hengst
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 157
Joined: Thu Jun 29, 2006 4:16 pm

Re: Using a number as a string vs numeral

Post by Randy Hengst » Sat Feb 23, 2013 12:14 am

I'd do this by naming the graphics with a word appended to the number... 1matrix, 2matrix, etc.

Then in your code

on mouseUp
set the backgroundColor of graphic ("23" & "Matrix") to "red"
end mouseUp

OR


on mouseUp
repeat with x = 1 to 100
set the backgroundColor of graphic (x & "Matrix") to "100,100,100"
end repeat
end mouseUp

magice
Posts: 457
Joined: Wed Mar 18, 2009 12:57 am

Re: Using a number as a string vs numeral

Post by magice » Sat Feb 23, 2013 6:15 am

I did something similar recently. Here is a little script I used to rename the objects by referencing their IDs.

Code: Select all

on mouseUp
   put 1760 into tIDNumAdj
   repeat with i = 1 to 704
      put "Image"&i into tNewName
      put "image id "&tIDNumAdj&" of card id 1002 of stack "&quote&"MapArea"&quote&" of stack "&quote&"D:/Projects/TileMapper/TileMapper.rev"&quote into tNewID
      set the name of tNewID to tNewName
      add 1 to tIDNumAdj
   end repeat
end mouseUp
Of course you will have to adjust it to your needs, repeat to 100 instead of 704, change the path to your stack, change the starting number for the IDs etc.

sritcp
Posts: 431
Joined: Tue Jun 05, 2012 5:38 pm

Re: Using a number as a string vs numeral

Post by sritcp » Sat Feb 23, 2013 7:44 pm

Thanks, Simon, I didn't think of ID!!
I guess adding

Code: Select all

if Q&N > 100 then
create graphic "g"&(Q&N - 100)
else create graphic "g"&Q&N
will rename the 10 (9?) graphics.

Magice, thanks for the code resetting the ID.
I assume we should begin with a larger number if there are other graphics already on the card.

Regards,
Sri.

Simon
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3901
Joined: Sat Mar 24, 2007 2:54 am

Re: Using a number as a string vs numeral

Post by Simon » Sat Feb 23, 2013 10:39 pm

Hi Sri,
I fixed the numbering of the graphic name (it's in the code already).
I would try that code on a new card just to see what it does.

Simon
I used to be a newbie but then I learned how to spell teh correctly and now I'm a noob!

sritcp
Posts: 431
Joined: Tue Jun 05, 2012 5:38 pm

Re: Using a number as a string vs numeral

Post by sritcp » Sat Feb 23, 2013 10:58 pm

Hi Simon:
This is what I see in your code:

Code: Select all

........
repeat with Q = 1 to 10
repeat with N = 0 to 9
create graphic "g"&Q&N
......
Doesn't that translate to g10, g11, ...., g109 ? I was trying to make the names go from g1 to g100.
I didn't realize that it would mess up the order of the ID numbers, I guess!

Thanks,
Sri.

Simon
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3901
Joined: Sat Mar 24, 2007 2:54 am

Re: Using a number as a string vs numeral

Post by Simon » Sat Feb 23, 2013 11:12 pm

Hi Sri,
Yes, it does start at 10.
I was just going for a sequential order. :)

Simon
I used to be a newbie but then I learned how to spell teh correctly and now I'm a noob!

Simon
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3901
Joined: Sat Mar 24, 2007 2:54 am

Re: Using a number as a string vs numeral

Post by Simon » Sat Feb 23, 2013 11:35 pm

Ahhh what the heck:

Code: Select all

on mouseUp
   put 0 into S
   put 0 into T
   put 0 into R
   repeat with Q = 1 to 10
      repeat with N = 0 to 9
         add 1 to R
         create graphic "g"&R
         set the rect of graphic ("g"&R) to S,T,S +25,T+25
         add 25 to S
      end repeat
      add 25 to T
      put 0 into S
   end repeat
end mouseUp
There's your 1-100

Simon
I used to be a newbie but then I learned how to spell teh correctly and now I'm a noob!

Klaus
Posts: 14199
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: Using a number as a string vs numeral

Post by Klaus » Sun Feb 24, 2013 11:29 am

Hi Sri,
sritcp wrote:

Code: Select all

........
repeat with Q = 1 to 10
repeat with N = 0 to 9
create graphic "g"&Q&N
......
ALWAYS use parenthesis when "concatenating" object names!
...
-> create grc ("g" & Q & N)
...
Did I mention ALWAYS? :D


Best

Klaus

sritcp
Posts: 431
Joined: Tue Jun 05, 2012 5:38 pm

Re: Using a number as a string vs numeral

Post by sritcp » Sun Feb 24, 2013 6:24 pm

Thanks, Simon!

And, yes, Klaus, I must remember not to get sloppy.
Always parenthesize when concatenating object names!
And use space between & and its arguments (even though LiveCode is forgiving).
And comment my code liberally.
........

Regards,
Sri.

Post Reply