Turning string into array?

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:

Turning string into array?

Post by trevordevore » Sun Mar 15, 2015 3:51 pm

In LCB the split command works on lists. I'm looking for a handler that will convert a string into an array. For example, convert a URL query string of key/value pairs into an array.

Does such a handler exist yet or should I just execute LCS for the time being?
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: Turning string into array?

Post by LCMark » Sun Mar 15, 2015 4:09 pm

@trevordevore: There isn't an equivalent of 'split <String> by <x> and <y>' yet. You could use somthing along these lines though directly in LCB:

Code: Select all

handler SplitStringIntoArray(in pString as String, in pKeyDel as String, in pValueDel as string) as Array
    variable tResult as Array
    put the empty array into tResult

    variable tKeyValue as String
    split pString by pKeyDel
    repeat for each element tKeyValue in the result
        split tKeyValue by pValueDel
        put element 2 of the result into tArray[element 1 of the result]
    end repeat

    return tResult
end handler

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: Turning string into array?

Post by trevordevore » Sun Mar 15, 2015 4:28 pm

Thanks Mark. I just changed tArray to tResult in the repeat loop (and made some syntax changes as I'm using develop) and got it working (see below). Looking at the function I noticed that you are using 'the result' as the target of the repeat loop and then 'the result' within the repeat loop as well. It would appear that the value of the target object of a repeat loop is captured when the repeat loop begins and that modifying it during the repeat loop doesn't affect the repeat loop. Is that correct?

Code: Select all

handler SplitStringIntoArray(in pString as String, in pKeyDel as String, in pValueDel as String) returns Array
	variable tResult as Array
	put the empty array into tResult

	variable tKeyValue as String
	split pString by pKeyDel
		
	repeat for each element tKeyValue in the result
		split tKeyValue by pValueDel
		put element 2 of the result into tResult[element 1 of the result]
	end repeat

	return tResult
end handler
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: Turning string into array?

Post by LCMark » Sun Mar 15, 2015 4:35 pm

Yes, the container in a repeat for each is an input argument and so 'copies' the value.

'the result' is a read-only expression and is always the return value of the previously executed command (control structures are ignored) (handlers returning nothing set the result to 'undefined').

n.allan
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 153
Joined: Mon Mar 12, 2007 12:06 pm

Re: Turning string into array?

Post by n.allan » Mon Mar 16, 2015 8:51 am

In the above LCB script, You defined and initialised the variable tResult as Array with "the empty array" but you have not done it for variable tValue as String? Is it only Lists and Array variables that need to be initialised?

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

Re: Turning string into array?

Post by LCMark » Mon Mar 16, 2015 9:14 am

@n.allan: The 'tKeyValue' gets initialized by the repeat for each statement. On each iteration of the loop, repeat for each puts the next element into the iterand variable.

Post Reply

Return to “LiveCode Builder”