How can we dynamically create variable names from changing value "x" on a loop?

Richard Gaskin ambassador at
Mon Nov 7 11:52:55 EST 2016

Ben Rubinstein wrote:

 > So the typical routine is something like
 > 	do makeAccessVars("vi", line 1 of tTSVdata)

There's your first bug right there - that should be:

   	do makeAccessVars("emacs", line 1 of tTSVdata)

(Ducking from Mark Wieder who will no doubt groan at the pun <g>)

But more seriously, the meat here is this:

 >   doSomething item viUserID of tRec, item viUserName of tRec


 >   doSomething aData["User ID"], aData["User Name"]

On the one hand, each array access requires a small amount of overhead 
to run the key through a hash to find the value's address.  However, 
that overhead is pretty small as hashes go, since it's not using 
anything heavy-duty like a cryptographic hash (no sha1Digest or even 
md5Digest), but some very small bit-shifting hash that only works as 
hard as it needs to to distribute addresses somewhat uniformly across 
internal buckets of addresses.

On the other hand, any chunk expression like "item viUserID of tRec" 
will require tRec to be traversed from its beginning, evaluating each 
character along the way, counting item delimiters as it goes.

Of course the loop you have there is much better than "repeat with", 
since at least each line itself is efficiently isolated for the 
item-test traversal.   But within each line traversal is still needed to 
identify items.

The cases I've seen where chunk expressions can outperform arrays tend 
to be those with a large number of lines and a small number of items, in 
which each item is itself a fairly short string.  And even then, other 
specifics about the data can come into play affecting measurable outcomes.

But given how lean the hash used for arrays is, I've found only a 
relatively small number of such cases.

The bigger difference by far will be with loading whichever structure 
you use.  While it does indeed require more or less the same 
character-by-character evaluation to split a chunk into an array, when 
the alternative leaves you with "do" you're up against the performance 
weaknesses inherent in it, which require dynamic evaluation of the 
expression and its context.

It takes a fair bit of exploration to find anything slower than "do" 
(though I'm sure there are a few cases if we look hard enough).

"do" is great for those rare moments when we truly have no alternative.

And since HyperCard didn't offer arrays, that was usually every day. :)

But arrays are a natural fit for cases where not only elements within a 
collection are variables, but also the names of those elements as well.

Indeed, that's the use case they were introduced to support.

  Richard Gaskin
  Fourth World Systems
  Software Design and Development for the Desktop, Mobile, and the Web
  Ambassador at      

More information about the Use-livecode mailing list