Most efficient way to update array stored in a list

LiveCode Builder is a language for extending LiveCode's capabilities, creating new object types as Widgets, and libraries that access lower-level APIs in OSes, applications, and DLLs.

Moderators: LCMark, LCfraser

Post Reply
trevordevore
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 1005
Joined: Sat Apr 08, 2006 3:06 pm
Location: Overland Park, Kansas
Contact:

Most efficient way to update array stored in a list

Post by trevordevore » Tue Jun 16, 2015 6:17 pm

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?
Trevor DeVore
ScreenSteps - https://www.screensteps.com

LiveCode Repos - https://github.com/search?q=user%3Atrevordevore+topic:livecode
LiveCode Builder Repos - https://github.com/search?q=user%3Atrevordevore+topic:livecode-builder

peter-b
Posts: 182
Joined: Thu Nov 20, 2014 2:14 pm
Location: LiveCode Ltd.

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

Post by peter-b » Wed Jun 17, 2015 4:37 pm

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. :)
LiveCode Open Source Team — @PeterTBBrett — peter.brett@livecode.com

LCMark
Livecode Staff Member
Livecode Staff Member
Posts: 1212
Joined: Thu Apr 11, 2013 11:27 am

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

Post by LCMark » Wed Jun 17, 2015 5:13 pm

@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.

trevordevore
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 1005
Joined: Sat Apr 08, 2006 3:06 pm
Location: Overland Park, Kansas
Contact:

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

Post by trevordevore » Thu Jun 18, 2015 5:05 pm

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.
Trevor DeVore
ScreenSteps - https://www.screensteps.com

LiveCode Repos - https://github.com/search?q=user%3Atrevordevore+topic:livecode
LiveCode Builder Repos - https://github.com/search?q=user%3Atrevordevore+topic:livecode-builder

LCMark
Livecode Staff Member
Livecode Staff Member
Posts: 1212
Joined: Thu Apr 11, 2013 11:27 am

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

Post by LCMark » Fri Jun 19, 2015 9:39 am

@trevordevore: Alternatively, just use the index into the list as the 'key' which is returned by your GetObjectArray function.

trevordevore
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 1005
Joined: Sat Apr 08, 2006 3:06 pm
Location: Overland Park, Kansas
Contact:

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

Post by trevordevore » Fri Jun 19, 2015 1:35 pm

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.
Trevor DeVore
ScreenSteps - https://www.screensteps.com

LiveCode Repos - https://github.com/search?q=user%3Atrevordevore+topic:livecode
LiveCode Builder Repos - https://github.com/search?q=user%3Atrevordevore+topic:livecode-builder

trevordevore
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 1005
Joined: Sat Apr 08, 2006 3:06 pm
Location: Overland Park, Kansas
Contact:

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

Post by trevordevore » Fri Jun 19, 2015 2:04 pm

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"] ]
Trevor DeVore
ScreenSteps - https://www.screensteps.com

LiveCode Repos - https://github.com/search?q=user%3Atrevordevore+topic:livecode
LiveCode Builder Repos - https://github.com/search?q=user%3Atrevordevore+topic:livecode-builder

Post Reply

Return to “LiveCode Builder”