convert array to tab delimited data

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: Klaus, FourthWorld, heatherlaine, kevinmiller

Post Reply
inbo712
Posts: 2
Joined: Fri Nov 10, 2017 6:12 pm

convert array to tab delimited data

Post by inbo712 » Thu Jan 10, 2019 10:24 pm

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?

mrcoollion
Posts: 396
Joined: Thu Sep 11, 2014 1:49 pm
Location: The Netherlands

Re: convert array to tab delimited data

Post by mrcoollion » Fri Jan 11, 2019 11:29 am

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

sphere
Posts: 860
Joined: Sat Sep 27, 2014 10:32 am
Location: Earth, Except when i Jump

Re: convert array to tab delimited data

Post by sphere » Fri Jan 11, 2019 1:15 pm

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
2018 Worldwide Mobile Market share: Android 84.4% !!!, iOs 15.1%, others 0.1%

Thierry
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 625
Joined: Wed Nov 22, 2006 3:42 pm
Location: France
Contact:

Re: convert array to tab delimited data

Post by Thierry » Fri Jan 11, 2019 2:11 pm

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
Thierry Douez - https://sunny-tdz.com
Pourquoi tant de notes lorsqu'il suffit de jouer les plus belles... [Barbara]

bogs
Posts: 3545
Joined: Sat Feb 25, 2017 10:45 pm

Re: convert array to tab delimited data

Post by bogs » Fri Jan 11, 2019 3:48 pm

Thierry, wonderful to see you post, it has been much too long!
Image

inbo712
Posts: 2
Joined: Fri Nov 10, 2017 6:12 pm

Re: convert array to tab delimited data

Post by inbo712 » Fri Jan 11, 2019 4:03 pm

Thierry,

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

Klaus
Posts: 11056
Joined: Sat Apr 08, 2006 8:41 am
Location: Germany
Contact:

Re: convert array to tab delimited data

Post by Klaus » Fri Jan 11, 2019 4:44 pm

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.

Thierry
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 625
Joined: Wed Nov 22, 2006 3:42 pm
Location: France
Contact:

Re: convert array to tab delimited data

Post by Thierry » Sat Jan 12, 2019 9:16 am

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
Thierry Douez - https://sunny-tdz.com
Pourquoi tant de notes lorsqu'il suffit de jouer les plus belles... [Barbara]

[-hh]
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 2067
Joined: Thu Feb 28, 2013 11:52 pm
Location: Göttingen, DE

Re: convert array to tab delimited data

Post by [-hh] » Mon Jan 14, 2019 9:26 pm

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!
shiftLock happens

Thierry
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 625
Joined: Wed Nov 22, 2006 3:42 pm
Location: France
Contact:

Re: convert array to tab delimited data

Post by Thierry » Tue Jan 15, 2019 9:03 am

[-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 622 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
Thierry Douez - https://sunny-tdz.com
Pourquoi tant de notes lorsqu'il suffit de jouer les plus belles... [Barbara]

bogs
Posts: 3545
Joined: Sat Feb 25, 2017 10:45 pm

Re: convert array to tab delimited data

Post by bogs » Tue Jan 15, 2019 12:29 pm

:shock:
Thierry, that is pure awesome, both sentiment and code
Image
Image

Post Reply

Return to “Getting Started with LiveCode - Complete Beginners”