Page 1 of 1

Using a number as a string vs numeral

Posted: Fri Feb 22, 2013 4:26 pm
by sritcp
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.

Re: Using a number as a string vs numeral

Posted: Fri Feb 22, 2013 4:32 pm
by Klaus
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

Re: Using a number as a string vs numeral

Posted: Fri Feb 22, 2013 4:42 pm
by dunbarx
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

Re: Using a number as a string vs numeral

Posted: Fri Feb 22, 2013 7:23 pm
by paul_gr
Hi Sri,
Using a number as a name is something you don't do in any programming language, not just Livecode.

Paul

Re: Using a number as a string vs numeral

Posted: Fri Feb 22, 2013 8:59 pm
by sritcp
Yes, I get it!

Thanks,
Sri.

P.S. Namaste to you, Klaus!

Re: Using a number as a string vs numeral

Posted: Fri Feb 22, 2013 11:12 pm
by Simon
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

Re: Using a number as a string vs numeral

Posted: Sat Feb 23, 2013 12:14 am
by Randy Hengst
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

Re: Using a number as a string vs numeral

Posted: Sat Feb 23, 2013 6:15 am
by magice
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.

Re: Using a number as a string vs numeral

Posted: Sat Feb 23, 2013 7:44 pm
by sritcp
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.

Re: Using a number as a string vs numeral

Posted: Sat Feb 23, 2013 10:39 pm
by Simon
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

Re: Using a number as a string vs numeral

Posted: Sat Feb 23, 2013 10:58 pm
by sritcp
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.

Re: Using a number as a string vs numeral

Posted: Sat Feb 23, 2013 11:12 pm
by Simon
Hi Sri,
Yes, it does start at 10.
I was just going for a sequential order. :)

Simon

Re: Using a number as a string vs numeral

Posted: Sat Feb 23, 2013 11:35 pm
by Simon
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

Re: Using a number as a string vs numeral

Posted: Sun Feb 24, 2013 11:29 am
by Klaus
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

Re: Using a number as a string vs numeral

Posted: Sun Feb 24, 2013 6:24 pm
by sritcp
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.