Page 1 of 1

building a dynamic var [Solved]

Posted: Mon Jun 15, 2015 4:14 pm
by atout66
Hi to all,

There is something I'm missing with the construction of a dynamic var.
Look the code below:

Code: Select all

 put "2" into lItem 
put "0" into les1
put "0" into les2
put "0" into les3
repeat with zzz = 1 to 3 -- just a test
put "les"&zzz into leTest -- build <les2> for example
if last char of leTest is lItem then
add 1 to leTest-- ERROR ! LC suggess leTest is numeric? and it is in that case
I also tried without the quotes like:

Code: Select all

put 0 into les1
but it doesn't help...

Thanks in advance if someone has an idea ;-)

Regards, Jean-Paul.

Re: building a dynamic var

Posted: Mon Jun 15, 2015 4:46 pm
by Klaus
Bonjour Jean-Paul,

in case like these you need to:
1. use an array! :D

Code: Select all

...
## Hint: No need to QUOTE numbers!
put 2 into lItem 
put 0 into tArray["les1"]
put 0 into tArray["les2"]
put 0 into tArray["les3"]
repeat with zzz = 1 to 3 
   put "les"&zzz into leTest
   if last char of leTest = lItem then
     add 1 to tArray[leTest]
   end if
end repet
...
2. DO IT!
...
do ("add 1 to" && leTest)
...

Best

Klaus

Re: building a dynamic var

Posted: Mon Jun 15, 2015 5:03 pm
by atout66
Ah, that's clever an array, I got it;-)

Thanks Klaus,
Regards, Jean-Paul.

Re: building a dynamic var

Posted: Mon Jun 15, 2015 5:12 pm
by dave.kilroy
Hi Jean-Paul - I think the problem is that you are trying to do a mathematical process on a string - leTest is a string and therefore the last char of it will be a string (even though it's a number!). If you were to separate it out, as in "put the last char of leTest into tVar" then tVar could be interpreted separately by the engine as a number - and then you could do maths stuff with it...

But in my example I haven't bothered with that as zzz already holds the number we need...

Code: Select all

on mouseUp
   put "2" into tItem 
   put "0" into les1
   put "0" into les2
   put "0" into les3
   repeat with zzz = 1 to 3 -- just a test
      put "les" & zzz into leTest -- build <les2> for example
      if zzz = tItem then replace (the last char of leTest) with (zzz + 1) in leTest
   end repeat
end mouseUp
EDIT 1: Aha speedy Klaus strikes again :) - I got distracted responding to this and got pipped-to-the-post!
EDIT 2: and I see that you were after something different to what I thought you wanted - so Klaus is doubly right and an array should be great for you...

Re: building a dynamic var

Posted: Mon Jun 15, 2015 5:19 pm
by atout66
Thanks to all of you, I realized how to deal with strings and numeric value ;-)

Regards, Jean-Paul.