Create multidimensional array from string? [SOLVED]

LiveCode is the premier environment for creating multi-platform solutions for all major operating systems - Windows, Mac OS X, Linux, the Web, Server environments and Mobile platforms. Brand new to LiveCode? Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

jacque
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 7228
Joined: Sat Apr 08, 2006 8:31 pm
Location: Minneapolis MN
Contact:

Re: Create multidimensional array from string?

Post by jacque » Sun Jul 11, 2021 4:32 pm

rkriesel wrote:
Sun Jul 11, 2021 5:24 am
I posted replies containing that in '12, '13, '18, and now '21, so familiar is good. The content has lasting value.

You're very welcome, Jacque. You might even see it again someday.
You may have to, since I couldn't find it in the dictionary anywhere.
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

stam
Posts: 2679
Joined: Sun Jun 04, 2006 9:39 pm
Location: London, UK

Re: Create multidimensional array from string? [SOLVED]

Post by stam » Sun Jul 11, 2021 6:45 pm

Well i am thankful for the help with this guys. It's a great tool for moving data between plain text and multidimensional arrays.


For anyone else that may need this, here is my abstracted function. Pass the text and the delimiter and pass true assignIndexes if you have multiple identically named keys with different values in order to have these keyed numerically. it returns a multidimensional array.
Not thoroughly tested in a huge variety of situations and probably not perfect but it's done the job for me... feel free to optimise further ;)

Code: Select all

function compoundSplit pText, pDelimiter, assignIndexes
    ## if there are duplicate keys (lines) with different values, these need to be numerically keyed -> pass assignIndexes as true
    local tArray, tValue, tSource, x
    lock screen
    filter pText without empty into tSource
    set the itemDelimiter to pDelimiter
    sort lines of tSource text ascending
    repeat with i = 1 to the number of lines of tSource
        if assignIndexes then // will key each value numerically
            add 1 to x
            put item 1 to -2 of line i of tSource  & pDelimiter & x & pDelimiter & item -1 of line i of tSource into line i of tSource
            if item 1 to -3 of line i of tSource  <> item 1 to -2 of line i + 1 of tSource  then
                put 0 into x
            end if
        end if
        put item 1 to -2 of line i of tSource into tArray // series of keys
        put item -1 of  line i of tSource into tValue // value
        split tArray by pDelimiter
        put tValue into it[tArray]
    end repeat
    return it
end compoundSplit 

Post Reply

Return to “Getting Started with LiveCode - Experienced Developers”