Page 2 of 2

Re: Create multidimensional array from string?

Posted: Sun Jul 11, 2021 4:32 pm
by jacque
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.

Re: Create multidimensional array from string? [SOLVED]

Posted: Sun Jul 11, 2021 6:45 pm
by stam
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