Page 1 of 1

Most efficient way to update array stored in a list

Posted: Tue Jun 16, 2015 6:17 pm
by trevordevore
I have a list of arrays. I need to perform a lookup to find the right array and then update a single key in the array. I'm wondering what the most efficient approach is. My initial attempt was to find the array I am targeting in a repeat loop, assign the found array to an <out> parameter, and the update the parameter.

Code: Select all

private handler _GetObjectArray(in pId as String, out rArray as Array) returns Boolean
		variable tObject as Array

		repeat for each element tObject in sObjects
			if tObject["uuid"] is pId then
				put tObject into rArray
				return true
			end if
		end repeat

		return false
	end handler

private handler _SetObjectProperty(in pId as String, in pProperty as String, in pValue as any) returns nothing
		variable tObject as Array
		if (_GetObjectArray(pId, tObject) is false) then
			_ThrowInvalidId(pId)
		end if
		put pValue into tObject[pProperty]
	end handler
That doesn't work, however, as the array value stored in the list is never updated. I guess I was hoping that an <out> parameter was passed by reference. Is there a way to pass a pointer to a variable so that it can be updated? Or do I have to update the entire array in the list element every time I want to update a single key?

Re: Most efficient way to update array stored in a list

Posted: Wed Jun 17, 2015 4:37 pm
by peter-b
LiveCode Builder only provides pass-by-value at the moment, sorry. We definitely need "mutable" parameters for handlers! It's on my shopping list for speeding up some of my LiveCode Builder stuff. :)

Re: Most efficient way to update array stored in a list

Posted: Wed Jun 17, 2015 5:13 pm
by LCMark
@trevordevore: Out variables just mean that the value at the end of the handler is copied into the container specified as the parameter on return - in particular, there's no referencing going on. LCB is the same as LCS in this regard (for what you tried to work, it would be necessary for you to be able to return a 'reference to a value').

In this case, I'd suggest just doing it as you would in LCS - iterate through the keys of the array, find the one which the right id, return the key and then mutate the array in the caller using the returned key.

Re: Most efficient way to update array stored in a list

Posted: Thu Jun 18, 2015 5:05 pm
by trevordevore
Thanks @LCMark and @peter-b.

@LCMark: I was trying to store arrays in a particular order which is why I was trying to store arrays in a list. I guess what I will do is use a list of ids for order (I need to be able to easily reorder) and then have a separate array variable for storing the actual data.

Re: Most efficient way to update array stored in a list

Posted: Fri Jun 19, 2015 9:39 am
by LCMark
@trevordevore: Alternatively, just use the index into the list as the 'key' which is returned by your GetObjectArray function.

Re: Most efficient way to update array stored in a list

Posted: Fri Jun 19, 2015 1:35 pm
by trevordevore
LCMark wrote:@trevordevore: Alternatively, just use the index into the list as the 'key' which is returned by your GetObjectArray function.
Am I able to update the array without extracting it, updating it, and then putting it back into the list? I couldn't figure out the syntax for targeting the array in the list.

Re: Most efficient way to update array stored in a list

Posted: Fri Jun 19, 2015 2:04 pm
by trevordevore
Never mind. I was overlooking the obvious.

Code: Select all

variable tList as List
put [] into tList

variable tA as Array
put the empty array into tA

put "value 1" into tA["name"]
push tA onto tList
log [tList[1]["name"]]
put "value 2" into tList[1]["name"]
log [ tList[1]["name"] ]