using variables as references?

LiveCode is the premier environment for creating multi-platform solutions for all major operating systems - Windows, Mac OS X, Linux, the Web, Server environments and Mobile platforms. Brand new to LiveCode? Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
heyvern
Posts: 30
Joined: Sun Feb 21, 2010 12:38 am

using variables as references?

Post by heyvern » Sun Mar 07, 2010 9:22 am

Is there a way to use variable references to replace or change text/strings the variable references?

For example in a repeat structure you use a variable to represent lines, words, array elements etc.

Code: Select all

repeat for each line k in myVariable
   replace "#" with empty in k
end repeat
However when you change the variable k the original is not changed. If I used

Code: Select all

repeat with i=1 to the lines in myVariable
I could use the number reference to change things in the original but the "for each" is way faster. Before picking up RR recently I have been using lua. In lua you can make a variable reference to another value and changing the variable reference changes the original. I suppose I could use a counter variable but the text processing is enormous and I worry about speed issues.

Mark
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 5150
Joined: Thu Feb 23, 2006 9:24 pm
Contact:

Re: using variables as references?

Post by Mark » Sun Mar 07, 2010 10:20 am

heyvern,

I think you have answered your own question. If you want to use a repeat for loop, you need to put the modified data into a new variable.

Best,

Mark
The biggest LiveCode group on Facebook: https://www.facebook.com/groups/livecode.developers
The book "Programming LiveCode for the Real Beginner"! Get it here! http://tinyurl.com/book-livecode

heyvern
Posts: 30
Joined: Sun Feb 21, 2010 12:38 am

Re: using variables as references?

Post by heyvern » Sun Mar 07, 2010 10:56 pm

That's what I thought.

There is one thing I have been trying to track down. I remember reading somewhere (can't find it now) in one of my searches that somehow using a "for each element k in myvar" the "k" actually represents the "real" array variable somehow. I may have imagined it of course. Wishful thinking.

This would make a great feature request. Imagine how great it would be to edit the "for each" variable and change the original reference? This would make using "for each" so much more powerful and eliminate using "counters" inside a repeat. Or being able to declare a complex reference to a long container description, for example a complex nested array, with a simple variable and be able to edit the original.

I have been so use to this in lua it's hard to break the habit. In lua you do this a lot. Sometimes the item you want to reference is a long complex line of code. By assigning it to a variable you can change the original with a very short bit of code.

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10043
Joined: Sat Apr 08, 2006 7:05 am
Contact:

Re: using variables as references?

Post by FourthWorld » Mon Mar 08, 2010 3:35 pm

You can still do that with repeat forms other than "repeat for each", such as "repeat with...":

Code: Select all

repeat with i = 1 to the number of lines of tData
  if item 3 of line i of tData is "bob" then
    DoSomethingWith line i of tData
  end if 
end repeat
For many years that was the only option every xTalk offered, until Rev added the "repeat for each" method, which is so much faster that it's usually well worth the extra couple lines needed to make a new variable:

Code: Select all

put empty into tNuData
repeat for each line tLine in tData
    if item 3 of tLine is "bob" then
       put tLine &cr after tNuData
    end if 
end repeat
delete last char of tNuData
While those two repeat forms look superficially similar, they're worlds apart in terms of what's going on in the engine.

With "repeat with" the engine cannot assume the data it's traversing has not changed each iteration through the loop, so when you tell it to get "line i of tData" it has to count line endings from the beginning of tData until it reaches i, and then hand that to you. This means that each time through the loop getting "line i" will be progressively slower.

In contrast, "repeat for each" works with the understanding that the data being traversed will not change, so it counts chunks and parses them out as it goes, keeping track of its place in the data through each iteration so that it doesn't need to start the count over from the beginning each time through the loop.

So while "repeat with" is progressively slower through each iteration, "repeat for each" scales linearly, allowing you to traverse large blocks of data in a small fraction of the time.

The other part of the magic of "repeat for each" is how "put after" has been optimized. In many 4GLs, appending a block of data in memory is a somewhat costly task as the old handle is destroyed and a new, larger one is created. But some years ago Dr. Raney optimized "put after" to involve much fewer operations, merely expanding the block of memory in place, so that it now runs about an order of magnitude faster than in earlier versions of the engine.

So using "repeat for each" with "put after" to build your modified data will usually save you significant time, for the low cost of about two extra lines of code.
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

Mark
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 5150
Joined: Thu Feb 23, 2006 9:24 pm
Contact:

Re: using variables as references?

Post by Mark » Mon Mar 08, 2010 3:37 pm

Richard,

That used to cause crashes. Are you sure those crashes belong to the past now?

Best,

Mark
The biggest LiveCode group on Facebook: https://www.facebook.com/groups/livecode.developers
The book "Programming LiveCode for the Real Beginner"! Get it here! http://tinyurl.com/book-livecode

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10043
Joined: Sat Apr 08, 2006 7:05 am
Contact:

Re: using variables as references?

Post by FourthWorld » Mon Mar 08, 2010 3:39 pm

Which part of that has caused crashes?

I've been using it extensively in WebMerge and most other programs I've shipped for years. In fact, judging from the comments on the use-rev list, it seems most folks are using "repeat for each" and enjoying the speed bump without issue.
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

heyvern
Posts: 30
Joined: Sun Feb 21, 2010 12:38 am

Re: using variables as references?

Post by heyvern » Wed Mar 10, 2010 4:47 am

I have been using "repeat for each" and "put after myNewVar" without any crashes.

Yes the speed difference is AMAZING. I couldn't have created my current application with OUT "repeat for each". The difference in speed is incalculable. With the "old slow" way it took up to 15-20 minutes to open and process one large 3-4mb file compared to just a second or two with "repeat for each". The exact same processing is happening in both versions I created. I started with the old way and got seriously stuck then switched to "repeat for each".

Post Reply