Can't pass array function result as parameter

James Spencer jspencer78 at mac.com
Thu Mar 30 22:13:11 EST 2006


On Mar 30, 2006, at 10:00 AM, David Burgun wrote:

> Hi.
>
> Depending on how you have coded it, there will be 2 copies of the  
> array, e.g.
>
> if you do this in the library:
>
> local sgLibArray
>
> function LibGetArray
> return sgLibArray
> end LibGetArray
>
>
> function LibChangeArray theArray,theKey,theData
> put theData into theArray[theKey]
> end LibChangeArray
>
>
> on mouseUp
> put LibGetArray() into myArray
>
> get LibChangeArray(myArray)
> end mouseUp
>
>
> Then there will be one copy of the array stored in gLibArray (in  
> the library stack) and one stored in myArray (in the client stack).  
> LibChangeArray() will change the copy in myArray and then return,  
> the mouseUp handler will return and myArray will be destroyed.
>
> In order to fix this you'd have to add the following line to  
> LibChangeArray:
>
> put theArray into sgLibArray
>
> AFAIK, this means the array is copied twice, there may be a way to  
> save this copying by use of the "@" keyword but if so I haven't  
> been able to figure out how to do it.

Thanks Dave but I didn't explain my problem well and while I will  
give the idea of different local variables existing in different  
handlers both in the library and in my code, I don't think this is  
the problem.  To try and explain again:

Assume LibGetArray() is a function like you have it above which  
returns an array.  Assume LibUseDifferentArray is a handler which is  
passed an array as a parameter and does something with it.  As far as  
the library is concerned there is no necessary connection between the  
two arrays. LibUseDifferentArray is defined as :

on LibUseDifferentArray pArray
   -- process the array
   -- ...
end


I have been trying to call these handlers as follows:

on mouseUp

   local tArray

   put LibGetArray() into tArray

   -- simplified modification of array:
   put tArray["Title"] into tTitle
   put " (DUPLICATE)" after tTitle
   put tTitle into tArray["Title"]

   LibUseDifferentArray tArray
end mouseUp

My problem is that while the debugger shows tArray in my mouseUp  
handler to be valid and shows it to have been correctly modified,  
when I step into the library code for LibUseDifferentArray, it shows  
pArray as empty and the processing fails.  In order to get it to work  
I have had to copy tArray to a new array, element by element:

on mouseUp

   local tArray, tNewArray
   put LibGetArray() into myArray

   -- simplified modification of array:
   put myArray["Title"] into tTitle
   put " (DUPLICATE)" after tTitle
   put tTitle into tNewArray["Title"]

   put tArray["SecondKey"] into tNewArray["SecondKey"]
   put tArray["ThirdKey"] into tNewArray["ThirdKey"]

   LibUseDifferentArray tNewArray
end mouseUp

One thing that I'm still exploring is that some of the elements of  
the array returned by LibGetArray() can be empty.  I'm not sure if  
the problem is related to this but its the best I've come up with.

James P. Spencer
Rochester, MN

jspencer78 at mac.com

"Badges??  We don't need no stinkin badges!"




More information about the use-livecode mailing list