How to populate an array from a csv list?
Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller
-
- Posts: 37
- Joined: Mon Nov 16, 2020 8:16 pm
How to populate an array from a csv list?
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.
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.
Re: How to populate an array from a csv list?
Hi Francesco,Francesco77 wrote: ↑Thu Dec 03, 2020 10:47 amI would like to create an array from a text list with several lines oft text items ...
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
!
SUNNY-TDZ.COM doesn't belong to me since 2021.
To contact me, use the Private messages. Merci.
!
SUNNY-TDZ.COM doesn't belong to me since 2021.
To contact me, use the Private messages. Merci.
!
-
- VIP Livecode Opensource Backer
- Posts: 9648
- Joined: Wed May 06, 2009 2:28 pm
- Location: New York, NY
Re: How to populate an array from a csv list?
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:
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:
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
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
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
Craig
-
- VIP Livecode Opensource Backer
- Posts: 9824
- Joined: Sat Apr 08, 2006 7:05 am
- Location: Los Angeles
- Contact:
Re: How to populate an array from a csv list?
How many lines are in that list?
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn
Re: How to populate an array from a csv list?
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:
This now REQUIRES Tab-CR text as data. Easy enough to convert :)
Call it from a button:
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"
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
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
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"
All code published by me here was created with Community Editions of LC (thus is GPLv3).
If you use it in closed source projects, or for the Apple AppStore, or with XCode
you'll violate some license terms - read your relevant EULAs & Licenses!
If you use it in closed source projects, or for the Apple AppStore, or with XCode
you'll violate some license terms - read your relevant EULAs & Licenses!
-
- Posts: 37
- Joined: Mon Nov 16, 2020 8:16 pm
Re: How to populate an array from a csv list?
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
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.
Thanks a lot for your help and advice,
With your help I have finally managed to get the data into my array variables
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.
-
- VIP Livecode Opensource Backer
- Posts: 9824
- Joined: Sat Apr 08, 2006 7:05 am
- Location: Los Angeles
- Contact:
Re: How to populate an array from a csv list?
Arrays are very powerful, and for some tasks they're the perfect choice.Francesco77 wrote: ↑Mon Dec 07, 2020 10:29 amAfter 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.
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
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn
Re: How to populate an array from a csv list?
And another way.
Jim Lambert
inData =
horse;Pferd;cavallo;лошадь;cheval
house;Haus;casa;домашний;maison
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