How to populate an array from a csv list?

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
Francesco77
Posts: 24
Joined: Mon Nov 16, 2020 8:16 pm

How to populate an array from a csv list?

Post by Francesco77 » Thu Dec 03, 2020 10:47 am

I would like to create an array from a text list with several lines oft text items which are separated by semicolons (;).

The lines look like:

horse;Pferd;cavallo;лошадь;cheval; ...;...
house;Haus;casa;домашний;maison; ...;...
... ...


I wonder how I can put these textlines into an array so that I can find the different entries by referring to the first item in the line.

For example:
put MyArray [horse] [3] = "cavallo"
or MyArray [house] [5] = "maison"

I already found out how to put the entries into an one dimensional array but I failed so far to put them in an array with more dimensions as needed for my little dictionary project.

Thanks in advance for your help and advice.

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

Re: How to populate an array from a csv list?

Post by Thierry » Thu Dec 03, 2020 11:06 am

Francesco77 wrote:
Thu Dec 03, 2020 10:47 am
I would like to create an array from a text list with several lines oft text items ...
Hi Francesco,

Hope you'll find these posts useful:

viewtopic.php?f=7&t=32020#p175195

http://forums.livecode.com/viewtopic.ph ... ne#p158579


Kind regards,

Thierry
Regex LiveCode sunnYrex
https://sunny-tdz.com

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 7289
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: How to populate an array from a csv list?

Post by dunbarx » Thu Dec 03, 2020 3:14 pm

I have not looked at Thierry's suggested posts, but he knows what he is talking about.

But I think you are trying to do something like this:

Code: Select all

on mouseup
   get "horse;Pferd;cavallo;лошадь;cheval"
   set the itemDel to ";"
   repeat with y = 2 to the number of items of it
      put item y of it into horse[y]  --A
      put item y of it into item 1 of it[y] --B
   end repeat
end mouseup
Line "A" is straightforward, but it requires that you explicity name your array variable. Line "B" is what I think you wanted, where the name of the array variable is taken from item 1 of the original string. I am not sure about this, because what I thought might work:

Code: Select all

on mouseup
   get "horse;Pferd;cavallo;лошадь;cheval"
   set the itemDel to ";"
   put item 1 of it into tkey
  
   repeat with y = 2 to the number of items of it
      do "put item" && y && "of" && it && "into" && tKey & "[" & y & "]"
   end repeat
end mouseup
In fact does not. Maybe someone will know how to extract a key from a list and make an array out of the rest of it?

Craig

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 8278
Joined: Sat Apr 08, 2006 7:05 am
Location: Los Angeles
Contact:

Re: How to populate an array from a csv list?

Post by FourthWorld » Thu Dec 03, 2020 5:18 pm

How many lines are in that list?
Richard Gaskin
Community volunteer LiveCode Community Liaison

LiveCode development, training, and consulting services: Fourth World Systems: http://FourthWorld.com
LiveCode User Group on Facebook : http://FaceBook.com/groups/LiveCodeUsers/

AxWald
Posts: 476
Joined: Thu Mar 06, 2014 2:57 pm

Re: How to populate an array from a csv list?

Post by AxWald » Fri Dec 04, 2020 1:12 pm

Hi,

a while ago I wrote a function to do this, and just last week I had to enhance it. So reading the OP I actually remembered it ;-)
It looks a bit complicated at first 'cause it's meant to be a universal library function, but it helps a lot:

Code: Select all

function Data2Array theData, theKeys, theKeyCol, theIgnoreKeyVal
   /* Use this to translate tabular data to a 3D array - myArr[x][y]
   The keys of the array can be auto-numbered (autoInc),
   or can be named according the contents of one of the columns (for 1st level),
   or according to a list of field names (2nd level). Or both.
   
   # "theData" (tab-del TEXT, ex. data from a db): the data to translate into a 3D array.
   # "theKeys" (optional, tab-del TEXT): the names of the 2nd level keys.
   # "theKeyCol" (optional, INT): # of column that will become the 1st level key.
   # "theIgnoreKeyVal" (optional, BOOL, default false): if "theKeyCol" is set, its value becomes the key.
   If you want this value omitted in the subkeys, set "theIgnoreKeyVal".
   
   (Hint: Column "theKeyCol" MUST NOT contain dupes, so best only use for an autoinc unique id!)
   
   Returns the desired array. Has no errors :)
   © axwald @ forums.livecode.com 2020, GPL v3   */
   
   set itemdel to tab
   put 0 into MyLCnt
   repeat for each line L in theData
      add 1 to myLCnt
      put 0 into myICnt
      repeat for each item I in L
         add 1 to myICnt
         switch
            case (theKeys is empty) and (theKeyCol is empty)                        --  myArr[1][1]
               put I into myArr[myLCnt][myICnt]
               break
            case (theKeys is not empty) and (theKeyCol is empty)             --  myArr[1][aColName]
               put I into myArr[myLCnt][(item myICnt of theKeys)]
               break
            case (theKeys is empty) and (theKeyCol is not empty)                 --  myArr[anID][1]
               if theIgnoreKeyVal then
                  if I = (item theKeyCol of L) then next repeat
               end if
               put I into myArr[(item theKeyCol of L)][myICnt]
               break
            case (theKeys is not empty) and (theKeyCol is not empty)       --  myArr[anID][aColName]
               if theIgnoreKeyVal then
                  if I = (item theKeyCol of L) then next repeat
               end if
               put I into myArr[(item theKeyCol of L)][(item myICnt of theKeys)]
               break
         end switch
      end repeat
   end repeat
   return myArr
end Data2Array
This now REQUIRES Tab-CR text as data. Easy enough to convert :)
Call it from a button:

Code: Select all

on mouseUp
   put fld "data_fld" into myData                            --  using the example of the OP (dressed)
   replace ";" with tab in myData                                  --  this here REQUIRES tab-CR text!
   put "english" & tab & "german" & tab & "italian" & tab & "russian" & tab & "french" into myKeys
   put data2Array(myData) into myArr_1                                        --  simple numbered keys
   put data2Array(myData,myKeys) into myArr_2                                        --  named subKeys
   put data2Array(myData,,1) into myArr_3                                          --  named main keys
   put data2Array(myData,myKeys,1) into myArr_4                              --  named main & sub keys
   put data2Array(myData,myKeys,1,true) into myArr_5      --  as above, but no key as additional value
   breakpoint
end mouseUp
You'll end in the debugger, check the resulting arrays in the "variables" tab!

Caution: The function isn't "production code" yet. There may be bugs still hidden.

Have fun!

Disclaimer: The code snippets are created with a community version of LC, and are thus GPL v3.
Before using it with a licensed version of LC or any hard- or software regulated by Apple be sure to have read & understood the following documents: "LiveCode EULA", esp. "1.h"; "GPL v3"; "Apple Media Services Terms and Conditions", esp. "G. ADDITIONAL APP STORE TERMS/ LICENSED APPLICATION END USER LICENSE AGREEMENT/ a. Scope of License"; "iPhone Developer Program License Agreement", esp. "7.3"
Livecode programming until the cat hits the fan ...

Francesco77
Posts: 24
Joined: Mon Nov 16, 2020 8:16 pm

Re: How to populate an array from a csv list?

Post by Francesco77 » Mon Dec 07, 2020 10:29 am

Sorry for the late reply, I was not at my desk for a few days.

Thanks a lot for your help and advice,

With your help I have finally managed to get the data into my array variables :D

After that I found out that even simpler solutions (read the data from comma separated text lines via "word" or "item") would do the trick for my needs.

It's astounding how many possibilities LC offers when working with lists, texts, words, items or chunks.
It seems that everything I need for my current project is possible with a few lines of code that even me as a beginner can understand and use.

Thanks again, you and this forum are a great help in learning LiveCode.

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 8278
Joined: Sat Apr 08, 2006 7:05 am
Location: Los Angeles
Contact:

Re: How to populate an array from a csv list?

Post by FourthWorld » Mon Dec 07, 2020 5:16 pm

Francesco77 wrote:
Mon Dec 07, 2020 10:29 am
After that I found out that even simpler solutions (read the data from comma separated text lines via "word" or "item") would do the trick for my needs.

It's astounding how many possibilities LC offers when working with lists, texts, words, items or chunks.
It seems that everything I need for my current project is possible with a few lines of code that even me as a beginner can understand and use.
Arrays are very powerful, and for some tasks they're the perfect choice.

But I'm very glad you've discovered what we broadly call "chunk expressions", the ability to work with text easily with common concepts like word, item, and line.

It's one of the distinguishing things about this xTalk family of languages, and LiveCode offers more flexibility with them than any other xTalk.
Richard Gaskin
Community volunteer LiveCode Community Liaison

LiveCode development, training, and consulting services: Fourth World Systems: http://FourthWorld.com
LiveCode User Group on Facebook : http://FaceBook.com/groups/LiveCodeUsers/

jiml
Posts: 231
Joined: Sat Dec 09, 2006 1:27 am
Location: Los Angeles

Re: How to populate an array from a csv list?

Post by jiml » Mon Dec 14, 2020 11:59 pm

And another way.

Jim Lambert

inData =
horse;Pferd;cavallo;лошадь;cheval
house;Haus;casa;домашний;maison

Code: Select all

function makeArray inData
   set the columndelimiter to ";"
   split inData by column
   put indata[1] into englishData
   delete variable inData[1]
   combine inData by column
   repeat with x = 1 to  the number of lines of englishData
      put line x of inData into cLine
      split cLine by column
      put cLine into outData[line x of englishData]  
   end repeat
   return outData
end makeArray

Post Reply

Return to “Getting Started with LiveCode - Complete Beginners”