You may have to, since I couldn't find it in the dictionary anywhere.
Create multidimensional array from string? [SOLVED]
Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller
-
- VIP Livecode Opensource Backer
- Posts: 7228
- Joined: Sat Apr 08, 2006 8:31 pm
- Location: Minneapolis MN
- Contact:
Re: Create multidimensional array from string?
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com
HyperActive Software | http://www.hyperactivesw.com
Re: Create multidimensional array from string? [SOLVED]
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
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