csv and items

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
pajito
Posts: 23
Joined: Thu Apr 13, 2017 8:08 pm

csv and items

Post by pajito » Thu Apr 13, 2017 8:16 pm

Hi evebody,
I am quite new to Livecode, so I face some problems even with basic staff.
I loading a csv into a field, now I need to get the 5 lines of this field and set them as labels of 5 buttons.
1st Problem: when I get item 1 it gives me the 3rd row of my field
2nd Problem: I can not get any other data except this second row.

Thanks In advance

Code: Select all

on mouseUp
   local tFile
   answer file "Select the File to load:"  with type "semicolon Separated Values|CSV"
   put it into tFile
   if tFile is empty then 
      exit mouseUp
   else
      put URL ("file:" & tFile) into field "fileIn"
      replace ";" with tab in field "fileIn"
      
      put item 1 of  field  "fileIn" into tVariable
      set the label of button "Super 1" to tVariable
      put item 2   of  field  "fileIn" into pVariable
      set the label of button "Super 2" to pVariable
      put item 3 of field "fileIn" into kVariable
      set the label of button "Super 3" to kVariable
      put item 4  of field "fileIn" into mVariable
      set the label of button "Super 4" to mVariable
      put item 5  of field "fileIn" into nVariable
      set the label of button "Super 5" to nVariable
      put item 6  of field "fileIn" into tVariable5
      set the label of button "Super 6" to tVariable5
      
      
      
      
   end if
end mouseUp



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

Re: csv and items

Post by Klaus » Thu Apr 13, 2017 8:38 pm

Hola pajito,

1. welcome to the form! :D
2. Looks like you did nto set the TAB as itemdelimiter, add this line:

Code: Select all

on mouseUp
   local tFile
   answer file "Select the File to load:"  with type "semicolon Separated Values|CSV"
   put it into tFile
   if tFile is empty then 
      exit mouseUp
   else
      put URL ("file:" & tFile) into field "fileIn"
      replace ";" with tab in field "fileIn"

      ### This one:
      set itemdel to TAB
      ## should do the trick :-)

      put item 1 of  field  "fileIn" into tVariable
      set the label of button "Super 1" to tVariable
      ## ...    
   end if
end mouseUp
Hint, you can use a repeat loop to save a lot of typing typing, instead of:

Code: Select all

...
put item 1 of  field  "fileIn" into tVariable
set the label of button "Super 1" to tVariable
put item 2   of  field  "fileIn" into pVariable
set the label of button "Super 2" to pVariable
put item 3 of field "fileIn" into kVariable
set the label of button "Super 3" to kVariable
put item 4  of field "fileIn" into mVariable
set the label of button "Super 4" to mVariable
put item 5  of field "fileIn" into nVariable
set the label of button "Super 5" to nVariable
put item 6  of field "fileIn" into tVariable5
set the label of button "Super 6" to tVariable5
...
Use:

Code: Select all

...
set itemdel to TAB
repeat with i = 1 to 6
  set the label of btn ("Super" && i) to item i of fld "filein"
end repeat
## Done :-)
...
Best

Klaus


P.S.
Since this is really NOT Off-Topic, I will move this thread to the "Beginners" section.

And here some great learning resources for the basics of Livecode:
http://www.hyperactivesw.com/revscriptc ... ences.html

pajito
Posts: 23
Joined: Thu Apr 13, 2017 8:08 pm

Re: csv and items

Post by pajito » Fri Apr 14, 2017 11:44 am

Hi again Klaus

I would like to thank you, everything worked smoothly. I used the trick you suggested and it saved me lot of time.
I already start reading the lessons, they look really helpful.

Thanks Again

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

Re: csv and items

Post by dunbarx » Sat Apr 15, 2017 11:58 pm

HI.

There is a never ending discussion about the pitfalls of csv. The issue is that a comma, which is a very common character, and which happens to be the default LC itemDelimiter, is used to separate data in text of that kind. These sorts of strings are used within LC all the time, as in: "dog,cat,bear,", where you control the structure of such strings. But if you grab a pice of text from somewhere else, you are likely to find commas randomly embedded, and run into problems trying to parse and keep track of everything.

Tabs are much better, are well understood and inherent in spreadSheets. I often use rare and invisible characters as itemDelimiters when I want to make sure that no text is likely to crash and burn with an item conflict.

Craig Newman

Mikey
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 555
Joined: Fri Jun 27, 2008 9:00 pm

Re: csv and items

Post by Mikey » Sun Apr 16, 2017 4:32 pm

There's also the LC CSV library on github:
https://github.com/macMikey/csvToText

Da_Elf
Posts: 287
Joined: Sun Apr 27, 2014 2:45 am

Re: csv and items

Post by Da_Elf » Mon Apr 24, 2017 7:01 pm

i dont like csv at all. im more of a tab seperator fan

Mikey
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 555
Joined: Fri Jun 27, 2008 9:00 pm

Re: csv and items

Post by Mikey » Mon Apr 24, 2017 7:18 pm

csv does not just include classic csv, with fields enclosed in quotes and separated by commas and lines terminated in cr's. csv includes tab-delimited colums with cr-delimited lines. In fact, due to RFC 4180, csv has come to mean any format that has a delimiter between columns and a separate delimiter between rows, whether or not each row or column is enclosed. The lc csv library that we have defaults to tab/cr.

I believe that I also included code to search a string for characters that are not present in the string, and propose those as column and line delimiters - so, for instance, if your string has both tab and cr in it, the routine might propose "J" and "#". You'll have to double-check me on that to confirm that I included it. If not, I'll have to dig it out and add it.

Post Reply

Return to “Getting Started with LiveCode - Complete Beginners”