Can I pass a reference to an array element?

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller

Post Reply
Tukcedo
Posts: 65
Joined: Fri Jun 27, 2014 9:43 pm

Can I pass a reference to an array element?

Post by Tukcedo » Sun Oct 26, 2014 8:01 pm

LiveCoders,

I read somewhere that you can't pass a reference to an array element, just the array itself. Is this still the case?

What I would like to do is assemble and position on screen a few images that the user selects. Each time the user chooses the "Select Image" button I increase a value and then call the function. It works fine for a situation where I want only a single image, but not when I use the array element.

This one is what I'd like but doesn't work:

Code: Select all

local pImageArray
local pImageCount

on doSomething
add 1 to pImageCount
call "selectImage album,pImageArray[pImageCount],100,35,480" on card "Library" # does NOT work: the 2nd value passed is empty
end doSomething
Elsewhere in the app this works:

Code: Select all

local pImage

on doSomething
call "selectImage album,pImage,100,35,480" on card "Library" # works fine: the 2nd value passed is "pImage"
end doSomething
Michel J.L. van der Kleij
Coding to help stray animals in the Philippines
Albert Foundation - http://albert.tukcedo.nl
Aklan Animal Rescue & Rehabilitation Center - http://aarrc.tukcedo.nl

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

Re: Can I pass a reference to an array element?

Post by FourthWorld » Sun Oct 26, 2014 8:28 pm

It works here, but initially did not appear to, because I was incrementing pImageCount beyond the number of elements in pImageArray. You may want to double-check the array to ensure you're obtaining a value for which there is a key.

It should work, because here you're passing by value rather than by reference (no "@"). In a recent version they also added the ability to pass array elements by reference, but that shouldn't affect the code you have here.
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

Tukcedo
Posts: 65
Joined: Fri Jun 27, 2014 9:43 pm

Re: Can I pass a reference to an array element?

Post by Tukcedo » Sun Oct 26, 2014 9:27 pm

Considering we're always dealing with associative arrays in LC (as far as I understand it) and so the elements are created as we go along, I've made sure (I'm putting an "answer" in the code) that pImageCount is actually "1" when we start. Therefore I believe that pImageArray[1] is a valid reference.

Perhaps I should try the '@' method then ...
Michel J.L. van der Kleij
Coding to help stray animals in the Philippines
Albert Foundation - http://albert.tukcedo.nl
Aklan Animal Rescue & Rehabilitation Center - http://aarrc.tukcedo.nl

bn
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 4174
Joined: Sun Jan 07, 2007 9:12 pm

Re: Can I pass a reference to an array element?

Post by bn » Sun Oct 26, 2014 9:38 pm

Hi Tukcedo,

try this in a button on card 1 of a stack

Code: Select all

on mouseUp
   put "2,1,3" into tLIstArray
   split tListArray by comma
   put any line of the keys of tListArray into tKey
   put tListArray[tKey] into tHowOften
   call "selectSomething tListArray[tHowOften]" of card "library"
end mouseUp
put this code into the card script of another card called "library"

Code: Select all

on selectSomething pItem
   beep pItem
end selectSomething
this works for me an I think it is a good approximation to your original code.

this has nothing to do with associative arrays or referenced arrays (@), trust Richard.

Kind regards
Bernd

Tukcedo
Posts: 65
Joined: Fri Jun 27, 2014 9:43 pm

Re: Can I pass a reference to an array element?

Post by Tukcedo » Sun Oct 26, 2014 10:36 pm

Interesting thought Bernd, but I don't quite understand what it does

Code: Select all

on mouseUp
   put "2,1,3" into tLIstArray
   split tListArray by comma                                          # so now we have tListArray[2], tListArray[1] and tListArray[3]
   put any line of the keys of tListArray into tKey            # looks like we're back at 2, 1, 3 again, the keys of tListArray, right?
   put tListArray[tKey] into tHowOften                           # shouldn't we put this in a loop?
   call "selectSomething tListArray[tHowOften]" of card "library"
end mouseUp
put this code into the card script of another card called "library"

Code: Select all

on selectSomething pItem                                   # pItem will be 2, 1, 3 when called in the order above, right?
   beep pItem
end selectSomething
Michel J.L. van der Kleij
Coding to help stray animals in the Philippines
Albert Foundation - http://albert.tukcedo.nl
Aklan Animal Rescue & Rehabilitation Center - http://aarrc.tukcedo.nl

bn
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 4174
Joined: Sun Jan 07, 2007 9:12 pm

Re: Can I pass a reference to an array element?

Post by bn » Sun Oct 26, 2014 10:51 pm

Hi Tukcedo,
split tListArray by comma # so now we have tListArray[2], tListArray[1] and tListArray[3]
no we have an array with the key -> value pairs of

1 -> 2
2 -> 1
3 -> 3
put any line of the keys of tListArray into tKey
this puts a random key of tListArray into tKey
put tListArray[tKey] into tHowOften
puts the value of the random key of tListArray into tHowOften, tHowOften is used as a key in tListArray[tHowOften] in the call line.

this all is to get some randomness into the array key (probably too complicated, just wanted to show that a variable used as a key of an array in a call statement works.


change in the example stack you made the code of the button of card 1 to

Code: Select all

local sListArray

on mouseUp
   put "2,1,3" into sListArray
   split sListArray by comma
   put any line of the keys of sListArray into tKey
   doCall tKey
end mouseUp

on doCall pKey
   put sListArray[pKey] into tHowOften
   call "selectSomething sListArray[tHowOften]" of card "library"
end doCall
and the script of card "library" to

Code: Select all

on selectSomething pItem
   put "" into field "fRes" of card "main"
   beep pItem
   put "you made me beep " & pItem & " times" & cr &  the milliseconds into field "fRes" of card "main"
end selectSomething
you would have to add a field "fRes" to card 1. I think this code is a bit cleaner and also adds the the array in a script local variable.

All this makes me believe that something else is going on in your case. It is not the array-thing.

Kind regards
Bernd

Tukcedo
Posts: 65
Joined: Fri Jun 27, 2014 9:43 pm

Re: Can I pass a reference to an array element?

Post by Tukcedo » Sun Oct 26, 2014 11:21 pm

Excellent, I'll try a little PoC tomorrow. Thx for the help!
Michel J.L. van der Kleij
Coding to help stray animals in the Philippines
Albert Foundation - http://albert.tukcedo.nl
Aklan Animal Rescue & Rehabilitation Center - http://aarrc.tukcedo.nl

Tukcedo
Posts: 65
Joined: Fri Jun 27, 2014 9:43 pm

Re: Can I pass a reference to an array element?

Post by Tukcedo » Mon Nov 03, 2014 7:31 pm

UPDATE:

After doing some tests it turned out that the mobilePickPhoto that I was using deeper down actually just wants a file name, rather than a variable. So after setting the array item to a name, it worked like a dream ...

Code: Select all

put "Image_" & pImageCount into pImageArray[pImageCount]
Michel J.L. van der Kleij
Coding to help stray animals in the Philippines
Albert Foundation - http://albert.tukcedo.nl
Aklan Animal Rescue & Rehabilitation Center - http://aarrc.tukcedo.nl

Post Reply