Generating Variables in a Repeat loop

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: heatherlaine, Klaus, FourthWorld, kevinmiller

Der-Schredder
Posts: 6
Joined: Mon Jun 11, 2018 12:20 pm

Generating Variables in a Repeat loop

Post by Der-Schredder » Mon Jun 11, 2018 12:43 pm

Hallo,
I started coding in Livecode about 1 week ago and now came across a very basic problem I can't seem to figure out. One of the "there has to be an easy solution, but I don't know what to google" problems.
Since my code is quite complicated at the moment, I created the simpelest verison of this problem.

I want to create a couple of variables called myVariable1, myVariable2, myVariable3, etc. automaticly. The content in them does't really matter at this point.
Therefore I wrote this script in a button (it doesn't even compile):

Code: Select all

on mouseUp
   repeat with x=1 to 3
      put "testContent" into ("myVariable"&x)
   end repeat
end mouseUp
I can't seem to figue out the right syntax, varying brackes and quotes doesn't seem to help. There will be 30 variables in my final script.

Best regards,
Leo

jmburnod
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 2176
Joined: Sat Dec 22, 2007 5:35 pm
Location: Genève
Contact:

Re: Generating Variables in a Repeat loop

Post by jmburnod » Mon Jun 11, 2018 1:38 pm

Hi Leo,
Welcome to this forum
This seems work

Code: Select all

on mouseUp
   repeat with x=1 to 3
      do "put" && "testContent  && the ticks into" && ("myVariable"&x)
      wait 2 ticks
   end repeat
   put myVariable1 & cr & myVariable2  & cr & myVariable3
end mouseUp
Best regards
Jean-Marc

Klaus
Posts: 10015
Joined: Sat Apr 08, 2006 8:41 am
Location: Germany
Contact:

Re: Generating Variables in a Repeat loop

Post by Klaus » Mon Jun 11, 2018 1:55 pm

Hallo Leo,

in case like this, it is often more effective to use an ARRAY!
DO has some overhead (=slower!), which ARRAYs do not have:

Code: Select all

...
repeat with i = 1 to 99
  put "some text..." && i into tArray[i]
end repeat
...
Et voila ONE Variable, created without DO and it is easily accessible:

Code: Select all

...
answer tArray[88]
## some text... 88
...
Best

Klaus

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 5328
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: Generating Variables in a Repeat loop

Post by dunbarx » Mon Jun 11, 2018 2:56 pm

What everyone said.

But it is instructive to grok what the "do" construction is all about, in that it provides another, second level of evaluation within the context of a line of code itself. If you are familiar with the way LC evaluates expressions before executing them, then you will benefit from being conversant with "do". You will know when you need it, as in this case.

This concept, which dates back to Hypercard days, is still valid, and finds uses in many other guises. It is worth playing with. HC did not have arrays, of course.

Craig Newman

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 5939
Joined: Sat Apr 08, 2006 7:05 am
Location: Los Angeles
Contact:

Re: Generating Variables in a Repeat loop

Post by FourthWorld » Mon Jun 11, 2018 3:29 pm

Whenever you need a collection of variables whose names cannot be known in advance, an array is the simplest and fastest solution.
Richard Gaskin
Community volunteer LiveCode Community Liaison

LiveCode development, training, and consulting services: Fourth World Systems: http://FourthWorld.com
LiveCode User Group on Facebook : http://FaceBook.com/groups/LiveCodeUsers/

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 5328
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: Generating Variables in a Repeat loop

Post by dunbarx » Mon Jun 11, 2018 4:08 pm

Whenever you need a collection of variables whose names cannot be known in advance, an array is the simplest and fastest solution.
Clearly. My point is that "do" and its extra evaluation still comprise an invaluable tool in LC.

@ Leo. Try this (and it goes back to Goodman). Make a field with several lines of text in it, but that one of those lines contains the word "xyz". In a button script:

Code: Select all

on mouseUp
   find "xyz" in fld 1
   put "AHA!" into the foundChunk  --fails
   do "put AHA! into" && the foundChunk  --AHA!
end mouseUp
LC, like HC before it, cannot quite understand the "two level" instruction framed by that line of code. This even though the code "reads" correctly, and seems sound. "Do", however, will, er, do its magic.

Craig

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 5939
Joined: Sat Apr 08, 2006 7:05 am
Location: Los Angeles
Contact:

Re: Generating Variables in a Repeat loop

Post by FourthWorld » Mon Jun 11, 2018 8:17 pm

dunbarx wrote:
Mon Jun 11, 2018 4:08 pm
Whenever you need a collection of variables whose names cannot be known in advance, an array is the simplest and fastest solution.
Clearly. My point is that "do" and its extra evaluation still comprise an invaluable tool in LC.
It is indeed useful, but mostly for those circumstances where we do not already have a purpose-built language element designed specifically for the task at hand. In some ways, "do" is the hammer that risks making everything look like a nail.

For example, this will execute just fine:

Code: Select all

do "go next card"
Of course your reaction would rightly be that using "do" there is unnecessary, since the "go" command works quite well without it.

The same is true of many other uses of "do" where built-in syntax has been added as a direct means of handling a task.

For those raised with HyperCard some 25 years ago, "do" was more frequently used because HyperTalk was a much more limited language. Among those limitations was that it had no arrays, so we developed habits of working around those limitations by whatever means we could find. We also had no custom properties, and had to come up with clever workarounds of using hidden fields, and we had no color so we had to get used to using an external that painted color overlays our our monochrome objects. It was a very different environment; a lot of fun in its day, but it's nice to have the rich scope of options we have today in LC.

Today's learners are in a world in which nearly every language they've seen supports arrays, which were added to this family of languages in LiveCode for cases like this. If we come across one I would be happy to learn from it.

Do is very valuable when arbitrary execution is needed and the goal is not already supported more directly.

But for those unaccustomed to it, its concatenation requirements and meta-execution nature arguably make it a bigger cognitive load than using purpose-built arrays for things like collections of variable names, it's measurably much slower to execute, and in Internet applications introduces one more point of potential injection that we need to think very carefully about.

Where no direct means is available, "do" is a godsend. But where we have a direct means, I've seen no cases where it offers a benefit beyond those already provided by alternate syntax designed for the task.
Richard Gaskin
Community volunteer LiveCode Community Liaison

LiveCode development, training, and consulting services: Fourth World Systems: http://FourthWorld.com
LiveCode User Group on Facebook : http://FaceBook.com/groups/LiveCodeUsers/

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 5328
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: Generating Variables in a Repeat loop

Post by dunbarx » Mon Jun 11, 2018 8:29 pm

Richard.

Sure, all that. You, Jacque and I have been hashing this over for three decades. And "do" may not be appropriate for beginners, because, well, see below.

Have you ever in your travels come across out a good explanation, that even I can understand, why

Code: Select all

 put "AHA!" into the foundChunk
is not understandable by HC/LC?

It is, after all, perfectly sound syntax. And both HC and LC were so accommodating, forgiving and clever in expression pre-evaluation.

Where is the missing link? In other words, where is the disconnect in such a construction? In yet other words, a talented and enthusiastic beginner would see nothing wrong with that line of code. Neither do I. This is all academic, but if I do not ask the question, I have to go back to work.

Craig

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 5939
Joined: Sat Apr 08, 2006 7:05 am
Location: Los Angeles
Contact:

Re: Generating Variables in a Repeat loop

Post by FourthWorld » Mon Jun 11, 2018 8:43 pm

dunbarx wrote:
Mon Jun 11, 2018 8:29 pm
Richard.

Sure, all that. You, Jacque and I have been hashing this over for three decades. And "do" may not be appropriate for beginners, because, well, see below.

Have you ever in your travels come across out a good explanation, that even I can understand, why

Code: Select all

 put "AHA!" into the foundChunk
is not understandable by HC/LC?
I think it's because the HyperTalk team chose to lie to scripters, in a way that the LC became obliged to maintain despite it being a lie. :)

Properties are both readable and writable, but functions are only readable, and foundChunk is a function.

The confusion arises with many such cases because the HyperTalk team chose to allow the use of "the" for some function calls (though adding to the confusion, not all), while also use "the" to address properties.

If the original team had simply stuck with using property syntax for properties and the parenthetical form for functions, this would never be a problem.

But by introducing this anomaly of allowing property syntax for function calls, and then exacerbating it by doing so inconsistently, from time to time we see cases like this where it's not clear whether a particular language element is a property or a function.

To help newcomers and my clients alike, I almost never use the property-syntax form of function calls. Not only does it help the reader distinguish each, but the parens make the function call stand out very clear for what it is when skimming to look for it, and it never looks like a property access if I'm skimming to find one of those.
Richard Gaskin
Community volunteer LiveCode Community Liaison

LiveCode development, training, and consulting services: Fourth World Systems: http://FourthWorld.com
LiveCode User Group on Facebook : http://FaceBook.com/groups/LiveCodeUsers/

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 5328
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: Generating Variables in a Repeat loop

Post by dunbarx » Mon Jun 11, 2018 9:02 pm

Richard.

Well what do you know? And after fter 31 years to boot.

This works:

Code: Select all

on mouseUp
   find "ddd" in fld 1
   put "25" into foundChunk()
end mouseUp
I am going right now to try it in HC. If it is true there as well, should I write to Danny Goodman and Dan Winkler?

Craig

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 5328
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: Generating Variables in a Repeat loop

Post by dunbarx » Mon Jun 11, 2018 9:14 pm

No dice. HC barks at "foundChunk()" but thinks nothing untoward about "the foundChunk".

Sadly, both forms are acceptable in HC. But the parenthetical form throws an error. LC seems to be just a little more robust, however that was implemented. I wonder if that was an issue that was on their radar.

So I think you were on to something. I bet Dan Winkler does not care.

Craig

SparkOut
Posts: 1732
Joined: Sun Sep 23, 2007 4:58 pm

Re: Generating Variables in a Repeat loop

Post by SparkOut » Mon Jun 11, 2018 9:32 pm

Just to add more mud to the water, value() is a useful* sidestep/hybrid/tweenstep twixt direct native syntax and the "do" construct.

* in appropriate circumstances

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 5328
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: Generating Variables in a Repeat loop

Post by dunbarx » Mon Jun 11, 2018 10:48 pm

Sparkout.

Terrific in forcing LC to evaluate an expression.

But you cannot:

Code: Select all

put "25" into the value of the foundChunk
or
put "25" into the value of foundChunk()
Does not compute.

Craig

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 5328
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: Generating Variables in a Repeat loop

Post by dunbarx » Mon Jun 11, 2018 10:51 pm

And just to muddy the waters, I am miffed that LC in some ways is even more tolerant of odd code constructions:

Code: Select all

 put "25" into the foundChunk()
Works fine. "The and "()" together. Heartwarming.

Craig

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 5939
Joined: Sat Apr 08, 2006 7:05 am
Location: Los Angeles
Contact:

Re: Generating Variables in a Repeat loop

Post by FourthWorld » Tue Jun 12, 2018 2:37 am

"Works" means "doesn't throw an error", or means "does something useful"?

If the latter, what happens with the foundChunk when no find command has been issued?
Richard Gaskin
Community volunteer LiveCode Community Liaison

LiveCode development, training, and consulting services: Fourth World Systems: http://FourthWorld.com
LiveCode User Group on Facebook : http://FaceBook.com/groups/LiveCodeUsers/

Post Reply

Return to “Getting Started with LiveCode - Complete Beginners”