Page 1 of 1

convert array to tab delimited data

Posted: Thu Jan 10, 2019 10:24 pm
by inbo712
I am trying to build a function that will take any array (no matter how many dimensions it might have) and display it as tab delimited data. I found this on livecode lessons: (how-do-i-display-an-array-in-human-readable-form) and tried modifying it to this:

function displayArray pArray, pIndent
# create the variable that loops through the keys in the array

local tKey
if pArray is an array then
# print information to indicate that we are entering a new nested level of the array
get return

# create the indentation
put tab after pIndent
repeat for each key tKey in pArray
# call displayArrayData with a nested array
put format("%s%s%s\n", pIndent, tKey, displayArray (pArray[tKey], pIndent)) after it
end repeat
delete the last char of it
put it into tlist
else
put pArray into tlist
end if
return tlist
end displayArray

This gets me close but the array is displayed in kind of an outline form. What I want is tab delimited. Does anyone have any thoughts?

Re: convert array to tab delimited data

Posted: Fri Jan 11, 2019 11:29 am
by mrcoollion
Hello inbo,

Do not know if this is the goal but you can very easy show your complete array as a treeview by using the Tree View Widget.

This is very easy .
Place the Tree View widget in a card and give it a name (e.g. myarrayview )
Then use the below statement to fill the tree view widget with your array data.

Code: Select all

set the arraydata of widget myarrayview  to yourarrayname
More info on Tree View Widget
http://livecode.wikia.com/wiki/Tree_view_widget
https://livecode.com/docs/9-0-0/compone ... ew-widget/

Regards,

Paul

Re: convert array to tab delimited data

Posted: Fri Jan 11, 2019 1:15 pm
by sphere

Code: Select all

set the itemdel to tab
now every piece of data separated by tab on a line will be an item

then create a repeat function to place every item of each line of your data to the field where you want it to be displayed

Re: convert array to tab delimited data

Posted: Fri Jan 11, 2019 2:11 pm
by Thierry
Hi,

Is this what you want?
sunnY- 2019-01-11 à 13.52.56.png
datas from:
http://lessons.livecode.com/m/4071/l/21 ... dable-form


If yes, then try this script:

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
and to call this function:

Code: Select all

on mouseUp
   -- use implicit delimiters settings ( d1 and d2)
   put flattenArray( tCustomerDataArray) into fld 1
   -- or set primaryDelimiter (d1) and use default's secondaryDelimiter
   --     put flattenArray( myArray, return )
   -- or set both delimiters
   --     put flattenArray( myArray, return , " - ")
end mouseUp
HTH,

Thierry

Re: convert array to tab delimited data

Posted: Fri Jan 11, 2019 3:48 pm
by bogs
Thierry, wonderful to see you post, it has been much too long!

Re: convert array to tab delimited data

Posted: Fri Jan 11, 2019 4:03 pm
by inbo712
Thierry,

Thank you for the flattenArray function. That was exactly what I needed.

Re: convert array to tab delimited data

Posted: Fri Jan 11, 2019 4:44 pm
by Klaus
Hi inbo712,

welcome to this wonderful forum! :D


Best

Klaus

Very personal note:
A little e.g. "Hello" would not have hurt for the first posting.

Re: convert array to tab delimited data

Posted: Sat Jan 12, 2019 9:16 am
by Thierry
inbo712 wrote: Thank you for the flattenArray function. That was exactly what I needed.
Glad that it works for you!
And welcome to the LC world...
bogs wrote: Thierry, wonderful to see you post, it has been much too long!
Nice to see again your legendary friendly and gentle attitude.
Much appreciated :)

Thierry

Re: convert array to tab delimited data

Posted: Mon Jan 14, 2019 9:26 pm
by [-hh]
This is VERY useful for callbacks from JavaScript, using LC's javascriptHandlers.
And also (the excellent technique) for use in LC Builder.

Welcome back Thierry, and thanks for that!

Re: convert array to tab delimited data

Posted: Tue Jan 15, 2019 9:03 am
by Thierry
[-hh] wrote:
Mon Jan 14, 2019 9:26 pm
VERY useful for callbacks from JavaScript, using LC's javascriptHandlers.
And also (the excellent technique) for use in LC Builder.

Welcome back Thierry, and thanks for that!
Thanks Herman,

sunnY- 2019-01-14 à 14.28.16.png
sunnY- 2019-01-14 à 14.28.16.png (188.6 KiB) Viewed 6645 times


If you are using it for this purpose, you might be interested with a more effective one,
hard coding the delimiters:

Code: Select all

function flattenArraySpeedy A, T, X
   if A is not an array then return T  & X & A & cr
   repeat for each key K in A
      put flattenArraySpeedy( A[K], T,  X & K & tab )  into T
   end repeat
end flattenArraySpeedy
and here is a function which does the reverse:

viewtopic.php?f=6&t=29798&p=158579&hili ... ne#p158579

Enjoy your day,

Thierry

Re: convert array to tab delimited data

Posted: Tue Jan 15, 2019 12:29 pm
by bogs
:shock:
Thierry, that is pure awesome, both sentiment and code
Image