Create multidimensional array from string? [SOLVED]
Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller
Create multidimensional array from string? [SOLVED]
Hi all,
is it possible to describe a multidimensional array in string and then use this to create an array to assign values to?
ie: if a string contains: "["key1"]["key2"]", is it possible to then create an array with these sub-keys and assign a value to ["key2"]?
The use-case i have in mind is for processing a return-delimited list where each line describes different keys & sub-keys of the same array, so would need to automate processing of each line and assign the final item of each line to the last sub-key of that particular branch...
I was thinking maybe processing the text and converting it somehow to JSON and using the JSON functions to convert to array, but the prospect of automating conversion of the text to JSON is at least as complex... I can't see a simpler way of doing this but perhaps you clever guys can!
Many thanks
Stam
is it possible to describe a multidimensional array in string and then use this to create an array to assign values to?
ie: if a string contains: "["key1"]["key2"]", is it possible to then create an array with these sub-keys and assign a value to ["key2"]?
The use-case i have in mind is for processing a return-delimited list where each line describes different keys & sub-keys of the same array, so would need to automate processing of each line and assign the final item of each line to the last sub-key of that particular branch...
I was thinking maybe processing the text and converting it somehow to JSON and using the JSON functions to convert to array, but the prospect of automating conversion of the text to JSON is at least as complex... I can't see a simpler way of doing this but perhaps you clever guys can!
Many thanks
Stam
Last edited by stam on Sun Jul 11, 2021 3:22 am, edited 3 times in total.
Re: Create multidimensional array from string?
Hi Stam,
well, the ITEMDELIMITER is your friend here, so it should not be too difficult to pase these strings.
Maybe you can provide a complete line of data?
Best
Klaus
well, the ITEMDELIMITER is your friend here, so it should not be too difficult to pase these strings.
Maybe you can provide a complete line of data?
Best
Klaus
-
- VIP Livecode Opensource Backer
- Posts: 9580
- Joined: Wed May 06, 2009 2:28 pm
- Location: New York, NY
Re: Create multidimensional array from string?
Stam.
Assuming you have in a field 1:
Craig
Assuming you have in a field 1:
Do you mean something like:dog
cat
mouse
Code: Select all
on mouseUp
get fld 1
put line 3 of it into mayArray[line 1 of it][line 2 of it][x]
end mouseUp
Re: Create multidimensional array from string?
Not quite... it's a bit more complex than that.
The structure of the free text is a bit like this:
root | key1 | content1
root | key1 | content2
root | key1 | key2 | content3
root | key1 | key2 | content4
root | key3 | content5
the key names are dynamically generated, so can't know in advance the key names.
I can certainly generate the text that would be in the SE for the array, ie for line 3 this would be:
root["key1"]["key2"]["content3"] -- but this is a string, not an array.
I thought that perhaps i could use a do merge() to put a value into the dynamically named array but this doesn't work.
If say we call the above string tArray, and the value to put in the destination key tValue:
doesn't produce any output - neither an error nor an array.
With quite a bit of work i could probably manage to automatically convert this to JSON, but because each subkey may or may not be an array that's going to be a pain to code, but would then be easy to convert an array...
the JSON would have to look like this:
this isn't critical, but i thought i'd work on problem i saw posted somewhere thinking yeh, this will be easy to convert to an array...... but i'm probably having several senior moments here...
The structure of the free text is a bit like this:
root | key1 | content1
root | key1 | content2
root | key1 | key2 | content3
root | key1 | key2 | content4
root | key3 | content5
the key names are dynamically generated, so can't know in advance the key names.
I can certainly generate the text that would be in the SE for the array, ie for line 3 this would be:
root["key1"]["key2"]["content3"] -- but this is a string, not an array.
I thought that perhaps i could use a do merge() to put a value into the dynamically named array but this doesn't work.
If say we call the above string tArray, and the value to put in the destination key tValue:
Code: Select all
do merge ("put" && quote & tValue & quote && "into" && tArray) -- where tArray = root["key1"]["key2"]["content3"]
With quite a bit of work i could probably manage to automatically convert this to JSON, but because each subkey may or may not be an array that's going to be a pain to code, but would then be easy to convert an array...
the JSON would have to look like this:
Code: Select all
{
"root" : [
{
"key1" : [
{"item" : "content1"},
{"item" : "content2"},
{"key2" : [
{"item" : "content3"},
{"item" : "content4"}
]},
]
},
{
"key3" : [
{"item" : "content5"}
]
}
]
}
-
- VIP Livecode Opensource Backer
- Posts: 9802
- Joined: Sat Apr 08, 2006 7:05 am
- Location: Los Angeles
- Contact:
Re: Create multidimensional array from string?
Variables can be used as keys.
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn
-
- VIP Livecode Opensource Backer
- Posts: 9580
- Joined: Wed May 06, 2009 2:28 pm
- Location: New York, NY
Re: Create multidimensional array from string?
@Richard
I sort of made that point in the line:
And I even used a non-existent variable to boot.
@Stam. Is what we both mentioned pertinent? If not, can you give a short snippet of code that you think will NOT do? And include a list of data that does not seem to work.
Craig
I sort of made that point in the line:
Code: Select all
put line 3 of it into mayArray[line 1 of it][line 2 of it][x]
@Stam. Is what we both mentioned pertinent? If not, can you give a short snippet of code that you think will NOT do? And include a list of data that does not seem to work.
Craig
Re: Create multidimensional array from string?
Hi, stam. Here's one way to transform your text into your array:
Code: Select all
command compoundSplit @r
repeat for each line l in r
split l by "|"
put "true" into it[l]
end repeat
put it into r
end compoundSplit
Code: Select all
on mouseUp
local t
put "root|key1|content1" & cr after t
put "root|key1|content2" & cr after t
put "root|key1|key2|content3" & cr after t
put "root|key1|key2|content4" & cr after t
put "root|key3|content5" & cr after t
breakpoint
compoundSplit t
breakpoint
end mouseUp
-- Dick
-
- VIP Livecode Opensource Backer
- Posts: 7215
- Joined: Sat Apr 08, 2006 8:31 pm
- Location: Minneapolis MN
- Contact:
Re: Create multidimensional array from string?
That's very clever. It works great but I'm not sure why. When a line is split, you get an array with three numeric keys:
1 root
2 key1
3 content1
Then somehow putting true into it[l] somehow creates an array with nested keys. How does that work?
1 root
2 key1
3 content1
Then somehow putting true into it[l] somehow creates an array with nested keys. How does that work?
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?
Hi Dick - this doesn't quite work. For one, it creates a key without a name with the value 'true'. For the other, it doesn't achieve the goal of putting the last value in the line as the content of the key. All keys have the content 'true'.rkriesel wrote: ↑Sat Jul 10, 2021 4:38 amHi, stam. Here's one way to transform your text into your array:Code: Select all
command compoundSplit @r repeat for each line l in r split l by "|" put "true" into it[l] end repeat put it into r end compoundSplit
This is some actual data to process:
Code: Select all
0125870X|Tests|ECHO|LVEDV|211 ml
0125870X|Tests|ECHO|LVEF|42.6%
0125870X|Tests|Bloods|FBC|101
0125870X|Tests|Bloods|NTproBNP|712 ng/L
5552771M|Tests|ECHO|LVEDV|99 ml
5552771M|Tests|ECHO|LVEF|59%
5552771M|Diagnoses|Recovered LV dysfunction
Using your algorithm, it almost works but i get this array back: apart from the stray key the top, the problem here is that the "results" are stored as a keys, instead of the values of the keys. All keys hold the value 'true' instead...
Can't quite see the way to correct this, other than iterating through the text and putting the last item of each line into the array that was created and then deleting the last key of each array. Seems less elegant!
Re: Create multidimensional array from string?
Hi Dick,
further to my previous post, i did manage to finally get it work as desired.
Still seems a bit like dark magic - your solution kind of worked but with some tweaking it did perfectly!
I should also say i had attempted something similar, but it failed spectacularly. My idea was to isolate the keys from the value, split the keys by the itemdelimiter and assign the value... but it didn't create a multidimensional array, just a array with keys with the name of the list of keys names containing the value at the end.
However using your suggestion i was able to finally get this to work:
Code: Select all
function compoundSplit pText
local tArray, tValue
set the itemDelimiter to "|"
repeat for each line tLine in pText
put item 1 to -2 of tLine into tArray // series of keys
put item -1 of tLine into tValue // value
split tArray by "|"
put tValue into it[tArray]
end repeat
return it
end compoundSplit
====================================
EDIT: Just FYI, this fails if 2 keys have the same name - if multiple values are assigned to he same key in separate lines, these need to be numerically keyed.
Last edited by stam on Sun Jul 11, 2021 12:50 am, edited 2 times in total.
Re: Create multidimensional array from string?
The following excerpts provide a quote from Mark Waddingham:
Does that answer your question, Jacque?
On Mar 12, 2012, at 10:19 AM, Pete wrote:
On Mar 12, 2012, at 1:18 PM, Dick Kriesel <dick.kriesel@mail.com> wrote:I'm wondering how you found about about this key values array feature? I
can't find any mention of it in the dictionary or the reference manual and
it sure seems like something that should be known!
I found it while looking through bug reports for arrays, in <http://quality.runrev.com/show_bug.cgi?id=7166> dated 2008-11-28. So, thanks be to Mark Waddingham and David Bovill for that.
For convenience, here's Mark's reply to David:
I agree, the feature's worth adding into the dictionary and manual.Thanks for the suggestion.
This is on the list to include for the next version. In that version with:
put tValue into tArray[tKey]
(indeed anywhere you can currently do tArray[tKey])
The meaning of 'tKey' will be extended:
1) If it is a string then it is as it is now
2) If it is a numerically keyed array starting at 1, then it will mean:
put tValue into tArray[tKey[1]][tKey[2]]...[tKey[n]]
where <n> is the number of elements in tKey
3) Otherwise it is an error.
This will allow you to construct an array containing the path to the key you
want to modify/fetch/delete dynamically.
-- Dick
-- Dick
Last edited by rkriesel on Sun Jul 11, 2021 2:49 am, edited 1 time in total.
Re: Create multidimensional array from string?
@stam: I wrote it first as you originally specified:
But when it was ready, I saw that in your sample data, you showed some key combinations that had more than one "content."The use-case i have in mind is for processing a return-delimited list where each line describes different keys & sub-keys of the same array, so would need to automate processing of each line and assign the final item of each line to the last sub-key of that particular branch...
So I mistakenly rewrote it before posting.
I'm glad it helped anyway.
-- Dick
-
- VIP Livecode Opensource Backer
- Posts: 7215
- Joined: Sat Apr 08, 2006 8:31 pm
- Location: Minneapolis MN
- Contact:
Re: Create multidimensional array from string?
Yes, it does. It even looks a bit familiar but I'd forgotten about it entirely. Thanks for the reminder, and for the trouble of looking up the reference.
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?
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.
-- Dick
Re: Create multidimensional array from string?
to us newcomers your experience is invaluable - can't thank you enough for sharing and for making this forum the amazing place it is...