Array Question

J. Landman Gay jacque at hyperactivesw.com
Mon Aug 10 00:05:52 EDT 2009


Len Morgan wrote:
> Can you use a variable name as the index of an array?  It doesn't appear 
> so or I'm doing something wrong.  Here's what I'm trying:
> 
> put 1 into tasks["Search"]
> put 2 into tasks["Reports"]
> 
> then in a menuPick handler:
> 
> on menuPick pChoice
>    goto cd tasks[pChoice]
> end menuPick
> 
> I need to do this because I have more cards than I have menu choices and 
> they are intermixed with the "real" ones and I don't want the user to go 
> directly to these "hidden" cards.

Variables will work. Besides the "goto" issue, where are you filling the 
array? If it's in the same script as the menupick handler then it needs 
to be declared as a script local variable at the top of the script:

   local tasks -- allows all handlers in this script to access it

   on fillArray
     put 1 into tasks["Search"]
     put 2 into tasks["Reports"]
   end fillArray

   on menuPick pChoice
     if the keys of tasks = "" then fillArray
     go to cd tasks[pChoice]
   end menuPick

Otherwise it won't be shared among the script handlers. If you are 
filling the tasks array in a different script, then you either need to 
make it a global variable or store it in a custom property (or property 
set) which you load into the tasks variable when you need it. In other 
words, there must be some way for all handlers to get access to the 
tasks variable. When it is only referenced within a handler, the 
variable will be set to empty when the handler begins and deleted when 
the handler ends.

If your stack design allows it, it's often easier to just name cards the 
same as the menu items. Since card names aren't visible to the user, you 
can call them anything, and then all you need to do is:

on menuPick pChoice
   go cd pChoice
end menuPick

It's a good idea to set the navigationArrows property to false if you 
don't want users paging through cards via the keyboard.

-- 
Jacqueline Landman Gay         |     jacque at hyperactivesw.com
HyperActive Software           |     http://www.hyperactivesw.com



More information about the use-livecode mailing list