Adding Data to an Array with Variables

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

Post Reply
JereMiami
Posts: 119
Joined: Mon Nov 03, 2014 12:17 am

Adding Data to an Array with Variables

Post by JereMiami » Tue Aug 03, 2021 3:28 pm

Alrighty-

I want to get the data of an array from a tree widget, add to it, and set it with the array with the added data. I first get the data from the tree widget:

Code: Select all

   
   get the arrayData of widget "chainTree"
   put it into tArrayData
   
No problem. Now I want to add data to that array while keeping the structure of that array. All elements should be empty. The problem is that the keys will be made up of variables, a combination of the items from the hilited line of the tree widget and the text of a field, like so:

Code: Select all

put the hilitedElement of widget "chainTree" into tItems
   repeat for each item x in tItems
      put "[" & quote & x & quote & "]" after tData
   end repeat
   put the text of fld "test" into tField
   put "[" & quote & tField & quote & "]" after tData
 
So how do I add the result of the new variable (tData = ["ExistingKeyA"]["ExistingKeyB"]["NewKeyC"]) to the existing array tArrayData, while preserving the structure of tArrayData and having all elements empty? I tried:

Code: Select all

   put tData after tArrayData
   set the arraydata of widget "chainTree" to tArrayData
... but it wipes the tree widget clean. Any thoughts?

SparkOut
Posts: 2839
Joined: Sun Sep 23, 2007 4:58 pm

Re: Adding Data to an Array with Variables

Post by SparkOut » Tue Aug 03, 2021 6:52 pm

To get info on how to make two arrays into one, look up the "union" command in the dictionary.

bn
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3990
Joined: Sun Jan 07, 2007 9:12 pm
Location: Bochum, Germany

Re: Adding Data to an Array with Variables

Post by bn » Tue Aug 03, 2021 9:19 pm

... but it wipes the tree widget clean. Any thoughts?
This is a bit tricky.

The data structure of hilitedElement is a comma delimited list whereas arraydata is an array.
The trick is to convert that comma delimited list into a subarray that you can stuff into arraydata at the right place with your changes.
Yout tried that but it turned out not be be a real array. And you can not simply append stuff to an array.

Here is a script that seems to work alright in my testing.
One thing it checks for is if the hilitedElement is "at the end" of the array. This is because you can select an element on the path to the last element and if you put your text there it will overwrite the rest of the path. I check for that and do an "answer" to alert the user but take no action to prevent that. See comments in script.

Code: Select all

on mouseUp
   local tData, tArrayData, tIndex, tKeys, tItems
   local tFieldText
   
   put the arrayData of widget "chainTree" into tArrayData
   put the hilitedElement of widget "chainTree" into tItems
   
   -- store the first key of tArrayData
   put item 1 of tItems into tIndex
   delete item 1 of tItems
   
   if char 1 of tItems is comma then
      delete char 1 of tItems
   end if
   
   -- turn tItems into an array to use as keys of a sub array
   split tItems by comma
   
   -- check if a selection is not the last key of the array
   put the keys of tArrayData[tIndex][tItems] into tKeys
   if tKeys is not empty then
      answer "your are overwriting arrray keys"
      -- do what you want maybe exit here.
   end if
   -- end check
   
   put the text of field "test" into tFieldText
   
   -- fill your new text into the working array
   -- or put it directly into the topLeve of tArrayData
   
   if tItems <> empty then
      -- selection is in a sub structure of the treeview
      put the text of fld "test" into tData[tItems]
      
      -- put your working array into the tArrayData array
      put tData into tArrayData[tIndex]
   else
      -- selection is level 1 of the treeview
      put tFieldText into tArrayData[tIndex]
   end if
   -- set arrayData of treeview
   set the arrayData of widget "chainTree" to tArrayData
   
end mouseUp
Kind regards
Bernd

JereMiami
Posts: 119
Joined: Mon Nov 03, 2014 12:17 am

Re: Adding Data to an Array with Variables

Post by JereMiami » Wed Aug 04, 2021 8:30 pm

Thank you!

I can chalk this up to a complete failure of understanding the "split" command when creating an array. Looking at your code, I also tweaked it so that it will add subarrays up to four levels.

Code: Select all

on mouseup
   
   put the arrayData of widget "chainTree" into tArrayData
   put the hilitedElement of widget "chainTree" into tItems
   put the number of items in tItems into tItemsNo
   if tItemsNo > 4 then
      answer "Limit 4 levels."
      exit mouseup
   end if
   
   put item 1 of tItems into tIndex1
   answer tIndex1
   put item 2 of tItems into tIndex2
   answer tIndex2
   put item 3 of tItems into tIndex3
   answer tIndex3
   put item 4 of tItems into tIndex4
   answer tIndex4
   
   put the text of field "test" into tFieldText
   split tFieldText by comma
   
   if tItemsNo = 1 then 
      split tIndex1 by comma
      put empty into tArrayData[tIndex1][tFieldText]
   end if
   if tItemsNo = 2 then
      split tIndex1 by comma
      split tIndex2 by comma
      put empty into tArrayData[tIndex1][tIndex2][tFieldText]
   end if 
   if tItemsNo = 3 then
      split tIndex1 by comma
      split tIndex2 by comma
      split tIndex3 by comma
      put empty into tArrayData[tIndex1][tIndex2][tIndex3][tFieldText]
   end if 
   if tItemsNo = 4 then
      split tIndex1 by comma
      split tIndex2 by comma
      split tIndex3 by comma
      split tIndex4 by comma
      put empty into tArrayData[tIndex1][tIndex2][tIndex3][tIndex4][tFieldText]
   end if 
   
   set the arrayData of widget "chainTree" to tArrayData
   
end mouseup

Thanks again, bernd- (and I also did not know "union" existed, though I did not use it in the code. So thanks also to SparkOut)

J.

bn
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3990
Joined: Sun Jan 07, 2007 9:12 pm
Location: Bochum, Germany

Re: Adding Data to an Array with Variables

Post by bn » Wed Aug 04, 2021 8:52 pm

Hi J.

I am not sure what you are doing here.

Code: Select all

   put the text of field "test" into tFieldText
   split tFieldText by comma
and then here

Code: Select all

   put empty into tArrayData[tIndex1][tFieldText]
You effectively create a new _key_ for tArrayData with tFieldText and as a value it is empty.
I thought you wanted to put tFieldText as _Value_ of the last available key.

My code puts tFieldText as _Value_ of the last key of a chain of keys.
?
Kind regards
Bernd

JereMiami
Posts: 119
Joined: Mon Nov 03, 2014 12:17 am

Re: Adding Data to an Array with Variables

Post by JereMiami » Wed Aug 04, 2021 9:01 pm

Yes. Your code places a value in the empty element of a subarray. I like it. I will use that for a different button.

This is for mobile so I don't want the user to use the tree widget's native "+" button. They are way too close to each other and there is not much you can do about that besides making them unmanageably larger. This code is an alternative to that. It creates a subarray (i.e. new key) under the hilited line with the name of the key derived from from the text in the field, while keeping the original structure of the tree widget. Stated differently, all elements in the array should be empty. The values will be supplied by using your code.

In short: My code adds an additional subarray to the hilited item item with an empty element, similar to the native "+" button. Your code will supply the value for that empty element.

bn
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3990
Joined: Sun Jan 07, 2007 9:12 pm
Location: Bochum, Germany

Re: Adding Data to an Array with Variables

Post by bn » Wed Aug 04, 2021 9:23 pm

Hi J.

Thanks for clarifying. I just wanted to make sure that your code does what you intended.

Kind regards
Bernd

rkriesel
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 118
Joined: Thu Apr 13, 2006 6:25 pm

Re: Adding Data to an Array with Variables

Post by rkriesel » Thu Aug 05, 2021 6:49 am

JereMiami wrote:
Tue Aug 03, 2021 3:28 pm
...So how do I add the result of the new variable (tData = ["ExistingKeyA"]["ExistingKeyB"]["NewKeyC"]) to the existing array tArrayData, while preserving the structure of tArrayData and having all elements empty?
...Any thoughts?
Hi, J.
Thanks to Bernd for his code, I can suggest a simplification that hides the complexity of distinguishing "index" from "items."

Code: Select all

command insertIntoWidgetAsSubkey pWidget, pText
   -- determine the insertion point
   local tKeys
   put the hilitedElement of pWidget into tKeys
   split tKeys using comma
   
   -- validate the insertion point
   get the arrayData of pWidget
   if it[tKeys] is an array then breakpoint
   
   -- insert
   put empty into it[tKeys][pText]
   set the arrayData of pWidget to it
end insertIntoWidgetAsSubkey
Then the coding technique that led to your variable "tIndex4" doesn't complicate the code and limit the depth of the array.
Does that work for you?
-- Dick

Post Reply

Return to “Getting Started with LiveCode - Experienced Developers”