Referencing a variable?

Jim Ault JimAultWins at yahoo.com
Sun Nov 27 22:25:12 EST 2005


You want to use one variable to hold the name of another variable.  You need
to think just a bit more correctly.  (Skip to the last 2 paragraphs if you
wish.)  First consider that xTalk thinks the following way.

put 123 into topDrawer
-- puts characters "123" into a variable named "topDrawer"
--from now on, using variable container 'topDrawer' is using "123"

add 1 to topDrawer 
-- add 1 (as a number) to topDrawer (as a number, if possible)
-- topDrawer is now  a string "124"
put char 3 of topDrawer into thirdChar
-- thirdChar is now a string "4"

---put "123" into "topDrawer" == error 'bad chunk destination'
put "123" into topDrawer -- ok, since topDrawer is not a reserved word
put 123 into topDrawer --again ok.  In xTalk these are the same

--but now to get the value of a variable named "jimsVarName"
--        and use that value as the name of another variable:
--run these script lines, step thru Variable Watcher and observe
on testMe
  breakpoint
  put "topDrawer" into jimsVarName  --   the STRING "topDrawer"
  put 123 into topDrawer --   set the VALUE of variable topDrawer
  put ("add 1 "& "to " & jimsVarName) into commandStrToExecute
  --note what Variable Watcher is showing at this point
  do commandStrToExecute  --   as if it were an xTalk script line
  answer "the variable " & jimsVarName & " now has chars (" & topDrawer &
")"
end testMe
--end script lines

If you use an array to do this, it is like using aBunchOfCubbyHoles[] where
aBunchOfCubbyHoles[2] is the second slot.  The Post Office uses an array on
a wall to hold mail.  POBox[1145] holds mail accessible using key 1145.  The
label on the front of the box is the ³key² we refer to in arrays.  Thus, if
there were 4 apartment mail boxes labeled ³Smith, Jones, Adams, Birch², the
array ³keys² would be those names, apartmentBox[³Smith²],
apartmentBox[³Jones²], apartmentBox[³Adams²], apartmentBox[³Birch²]...
holding mostly bills and junk mail (physical spam).

Hope this helps you and others.  Variable Watcher is a very valuable tool !
Jim Ault
Las Vegas

On 11/27/05 5:44 PM, "Ian Leigh" <ianl at mac.com> wrote:

> Many thanks Alex for giving me this solution. I don't understand why
> it will only work with the do command. If you try exactly the same
> command normally it doesn't work, why is that?
> 
> Cheers
> 
> Ian
> 
> 
> On 28 Nov 2005, at 01:19, Alex Tweedly wrote:
> 
>> Ian Leigh wrote:
>> 
>>> Hello all,
>>> 
>>> I would like to do the following:
>>> 
>>> Put a variable name into another buffer variable.
>>> Reference the actual variable but using the buffer variable.
>>> 
>>> So I only have the name of the variable in the buffer but I want
>>> to  put a value into the actual variable only by using the
>>> variable name  which is stored in the buffer.
>>> 
>>> Does this make sense and can it be done? I thought simply :
>>> 
>>> put 123 into "buffer"
>>> 
>>> might work but it doesn't work that way. I have also tried
>>> various  adds and used the value command too but I can't seem to
>>> figure it  out. Any insight would be appreciated.
>> 
>> Here's *an* answer - I think it might be the answer to the question
>> you were asking :-)
>> 
>> put 123 into myVariable    -- give it an initial value
>> put "myVariable" into otherVariable   -- put its name into another
>> variable
>> do "put 456 into " & otherVariable    -- use that other variable to
>> access the original one
>> put myVariable & cr after msg    -- and output it to check we
>> succeeded
>> 
>> this should put out 456, showing that the 3rd line did indeed do
>> what we wanted.
>> 
>> -- 
>> Alex Tweedly       http://www.tweedly.net
>> 
>> 
>> 
>> -- 
>> No virus found in this outgoing message.
>> Checked by AVG Free Edition.
>> Version: 7.1.362 / Virus Database: 267.13.8/183 - Release Date:
>> 25/11/2005
>> 
>> _______________________________________________
>> use-revolution mailing list
>> use-revolution at lists.runrev.com
>> Please visit this url to subscribe, unsubscribe and manage your
>> subscription preferences:
>> http://lists.runrev.com/mailman/listinfo/use-revolution
> 
> _______________________________________________
> use-revolution mailing list
> use-revolution at lists.runrev.com
> Please visit this url to subscribe, unsubscribe and manage your subscription
> preferences:
> http://lists.runrev.com/mailman/listinfo/use-revolution




More information about the use-livecode mailing list