Page 1 of 2

Enhanced "combine" command

Posted: Tue Sep 26, 2017 4:22 pm
by dunbarx
It has been well received by the team that the "combine" command be enhanced by allowing it to contain any number of delimiters, as opposed to the current 2. In this way an array of any depth, that is, when the keys of an array are associated with another array.

Or deeper yet.

QCC #20443, hopefully will be implemented soon. I would have great use for this.

Craig Newman

Re: Enhanced "combine" command

Posted: Tue Sep 26, 2017 5:52 pm
by FourthWorld
Except in the singular case where the plain-text representation would be transformed into an array where each end node contains only one element, columnar data is problematic for such expression.

Most arrays are hierarchical collections, which require something beyond simple delimiters.

For plain-text representations of hierarchical data LC already supports the two most commonly-used forms, JSON and XML.

What are some of the use-cases where those common formats would be less effective for representing hierarchical data than columnar formats?

Re: Enhanced "combine" command

Posted: Tue Sep 26, 2017 9:32 pm
by dunbarx
Richard.
commonly-used forms, JSON and XML.
In my own work, I never import or read such data from external sources. I create and store my own. I use arrays often, because of their compactness and intrinsic power.

But since all my arrays are built by myself, deconstructing either requires that I "read" them in the debugger, often setting a breakpoint just for the purpose, or changing them into an ordinary variable with the "combine" command.

If you have:

Code: Select all

on mouseUp
   put "10" into myArray["a"]["b"]["c"]["d"]["e"]
   answer myArray["a"]["b"]["c"]["d"]["e"]
end mouseUp
Well, that gets the "10" out.

What the enhanced combine command would do is allow me to transform "deeply nested" array variables into ordinary ones in one line:

Code: Select all

put 10 into myArray["a"]["w"]
put 20 into myArray["a"]["x"]
put 30 into myArray["a"]["y"]
put 40 into myArray["a"]["z"]
The new: "combine myArray with return and comma and comma" would yield:

a,w,10
a,x,20
a,y,30
a,z,40

This can all be done right now, but not in one line.

Craig

Re: Enhanced "combine" command

Posted: Wed Sep 27, 2017 12:18 am
by Thierry
dunbarx wrote: This can all be done right now, but not in one line.
Hi Craig,

you could try this little piece of pure livecode
and might be that you'll like it and use it before your request will be honored?

Code: Select all

function flattenArray A , T,  X
   if A is not an array then return T  & X & A & d1
   repeat for each key K in A
      put flattenArray( A[K],  T,  X & K & d2 )  into T
   end repeat
end flattenArray
and I test it this way:

Code: Select all

local  d1 -- primary delimiter
local d2  -- secondary delimiter

on mouseup
   local myArray
   put 10 into myArray["a"]["o"]
   put 30 into myArray["b"]["p"]
   put 40 into myArray["b"]["w"]
   put 20 into myArray["c"]["q"]
   put 50 into myArray["c"]["r"]
   put 60 into myArray["b"]["s"]
   put 70 into myArray["d"]
   put 42 into myArray["s"]["u"]["n"]["n"]["y"]
   
   put cr into d1
   put " - " into d2
   put flattenArray( myArray )
end mouseup
which should give you:
a - o - 10
b - w - 40
b - s - 60
b - p - 30
c - r - 50
c - q - 20
d - 70
s - u - n - n - y - 42
Thierry

Re: Enhanced "combine" command

Posted: Wed Sep 27, 2017 6:02 pm
by dunbarx
Thierry.

Lovely, and a good lesson in recursion.

But a bit more than one line. :wink:

Craig

Re: Enhanced "combine" command

Posted: Wed Sep 27, 2017 6:09 pm
by Thierry
dunbarx wrote: Lovely, and a good lesson in recursion.

But a bit more than one line. :wink:
Hi Craig,

Sorry, I'm getting old :)

If you ever use it and want to change some behavior,
I'll be happy to give a hand...

Regards,

Thierry

Re: Enhanced "combine" command

Posted: Wed Sep 27, 2017 6:31 pm
by FourthWorld
dunbarx wrote:Thierry.

Lovely, and a good lesson in recursion.

But a bit more than one line. :wink:
If implemented in the engine it would be far more lines. Either option can be called with one line. And Thierry's solution can be used today. :)

Re: Enhanced "combine" command

Posted: Wed Sep 27, 2017 10:00 pm
by dunbarx
All this is doable. No argument.

But it surely has to be admitted that the enhanced command is cleaner and more accessible. The delimiters live in that single line. There is a bit more work required to set them up in Thierry's gadget.

Because of its recursion, though, it does handle the "depth" issue nicely, escaping when there is no more "array" left to process. I like that a lot.

Craig

Re: Enhanced "combine" command

Posted: Wed Sep 27, 2017 10:55 pm
by Thierry
dunbarx wrote:All this is doable. No argument.

But it surely has to be admitted that the enhanced command is cleaner and more accessible.
The delimiters live in that single line.
There is a bit more work required to set them up in Thierry's gadget.
gadget? :shock:

So here is a variant, where you can call the function in 3 ways:

Code: Select all

-- use implicit delimiters settings ( d1 and d2)
put flattenArray( myArray)

-- or set primaryDelimiter (d1) and use default's secondaryDelimiter
put flattenArray( myArray, return )

-- or set both delimiters
put flattenArray( myArray, return , " - ")

Code: Select all

function flattenArray A , d1, d2, T,  X
   if T is empty then
      if d1 is empty then put cr into d1
      if d2 is empty then put tab into d2
   end if
   if A is not an array then return T  & X & A & d1
   repeat for each key K in A
      put flattenArray( A[K],  d1, d2, T,  X & K & d2 )  into T
   end repeat
end flattenArray

Regards,

Thierry

Re: Enhanced "combine" command

Posted: Wed Sep 27, 2017 11:34 pm
by dunbarx
Thierry.

"Gadget" is anything (not anyone) that is small, beautiful, and has class and character.

Craig

Re: Enhanced "combine" command

Posted: Thu Sep 28, 2017 12:07 am
by FourthWorld
dunbarx wrote:All this is doable. No argument.

But it surely has to be admitted that the enhanced command is cleaner and more accessible. The delimiters live in that single line. There is a bit more work required to set them up in Thierry's gadget.
True, coding requires code. But a quick copy-n-paste is all Thierry's handler needs.

The request for an engine version has been submitted, IIRC. There's a long queue ahead of it. In the meantime, what Thierry has kindly provided seems a nice solution that can be used today.
Because of its recursion, though, it does handle the "depth" issue nicely, escaping when there is no more "array" left to process. I like that a lot.
Thierry does good work.

Re: Enhanced "combine" command

Posted: Thu Sep 28, 2017 7:10 am
by Thierry
dunbarx wrote: "Gadget" is anything (not anyone) that is small, beautiful, and has class and character.
Oops, another word so-called 'false friends' :oops:
in French:
qui séduit par son caractère nouveau et original, mais qui n'est pas d'une grande utilité
As you might see, this have a 'useless' meaning in my country.


@Richard, thanks for your words, feeling as it's my birthday and Christmas time all together :roll:

@Craig, you do not like my second version?

Regards,

Thierry

Re: Enhanced "combine" command

Posted: Thu Sep 28, 2017 1:49 pm
by dunbarx
Thierry.

I do like your solution. A lot.

And "gadget", as I define it, is strictly my own definition. In the rest of the world, it is a small, often ad hoc machine or machine component that does some sort of task.

Craig.

Re: Enhanced "combine" command

Posted: Thu Sep 28, 2017 2:53 pm
by Thierry
dunbarx wrote: I do like your solution. A lot.
Glad that you find it useful.

And "gadget", as I define it, is strictly my own definition.
No worries.
As a non native English speaker,
I could tell you so many little stories which were a bit embarrassing for me
and funny afterwards :)

Thierry

Re: Enhanced "combine" command

Posted: Sat Sep 30, 2017 7:46 am
by SparkOut
Thierry once told me I should respect him because he is older than me. (Not sure by how much but it can't be that many years.)
I respect practically everybody. I admire Thierry.