Tab delimitated text import breaks if there is a comma

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: FourthWorld, heatherlaine, Klaus, kevinmiller

Post Reply
visionmill
Posts: 6
Joined: Fri Sep 12, 2014 2:32 pm

Tab delimitated text import breaks if there is a comma

Post by visionmill » Tue Sep 30, 2014 7:50 pm

Hi

I'm trying to import a tab delimited text file and extract the three columns. The first row contains the column headings. I can import the text fine but if there is a comma in the field livecode treats this as the end of the field.

My data is set up as follows:

Name Job Company
David CEO abc
Tim CEO, COO def

The first entry imports correctly, but the second one stops just before the comma and then doesn't import the company name

Here's my code:

Code: Select all

on mouseUp

   answer file "A text file" with type ("text files|txt|tTXT" & return & "all files|*|*")
   if it <> "" then
      put it into theFilePath
      put url ("file:" & theFilePath) into tCardNames
      
   else
      --no file was selected, or cancel was pressed
      beep
   end if

   
repeat with i = 2 to the number of lines of tCardNames
       put item 1 of line i  of tCardNames into tFields
     
       createNamedButton i, tFields
       
    end repeat
    put (i-1) into field fCSVButtons  //store how many buttons were created
    
end mouseUp


on createNamedButton n, aFields
   
   replace tab with return in aFields
   
   create button "btn"&(n-1)
   put item 1 of line 1 of aFields into aName
   put item 1 of line 2 of aFields into aTitle
      put item 1 of line 3 of aFields into aJob
   
   put cr into aTitleWrap
   put wordWrapped (aTitle, 25) after aTitleWrap
   set the label of it to aName &return & aTitleWrap&return & aJob
   set the width of it to 150
   set the height of it to 100
end createNamedButton
Why does having a comma in the field break the import? Is it because livecode treats the text file as a csv? Is there any way to stop this happening?

Thanks

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10043
Joined: Sat Apr 08, 2006 7:05 am
Contact:

Re: Tab delimitated text import breaks if there is a comma

Post by FourthWorld » Tue Sep 30, 2014 8:03 pm

The default item delimter is comma. If you want to parse by tab then add this before using any item-based chunk expressions:

set the itemdel to tab
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

Klaus
Posts: 14177
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: Tab delimitated text import breaks if there is a comma

Post by Klaus » Tue Sep 30, 2014 8:08 pm

Hi Visisonmill,

you are talking about a TAB delimited text, but you are not setting the ITEMDELIMITER to TAB in your script!?
The DEFAULT itemdelimiter is COMMA in Livecode! 8)

Try this:

Code: Select all

on mouseUp
   answer file "A text file" with type ("text files|txt|tTXT" & return & "all files|*|*")
   if it <> "" then
      put it into theFilePath
      put url ("file:" & theFilePath) into tCardNames
   else
      beep
     ## We must NOT carry on with the script in this case!
     EXIT mouseup
   end if

   ## !!
   set itemdel to TAB
   repeat with i = 2 to the number of lines of tCardNames
       put item 1 of line i of tCardNames into tFields
...
Best

Klaus

bn
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 4163
Joined: Sun Jan 07, 2007 9:12 pm

Re: Tab delimitated text import breaks if there is a comma

Post by bn » Tue Sep 30, 2014 8:44 pm

Hi VisionMill,

you actually don't really use tab as a delimiter except when you replace tab with return in handler "createNamedButton".

What you are using in mouseUp handler are lines not items. It is important to understand the difference. As Richard and Klaus have said the default itemDelimiter is comma.

this code should work with your data, note it does not use items.

Code: Select all

on mouseUp
   put field "fData" into tCardNames
   
   repeat with i = 2 to the number of lines of tCardNames
      put line i  of tCardNames into tFields
      
      createNamedButton i, tFields
      
   end repeat
   put (i-1) into field fCSVButtons  //store how many buttons were created
end mouseUp


on createNamedButton n, aFields
   
   replace tab with return in aFields
   
   create button "btn"&(n-1)
   put line 1 of aFields into aName
   put line 2 of aFields into aTitle
   put line 3 of aFields into aJob
   
   put cr into aTitleWrap
   --put wordWrapped (aTitle, 25) after aTitleWrap
   put aTitle after aTitleWrap
   set the label of it to aName &return & aTitleWrap&return & aJob
   set the width of it to 150
   set the height of it to 100
end createNamedButton
I had put your sample data into a field I called "fData" and made shure that it is tab delimited. And I omitted your wordWrapped function. See the code.

Kind regards
Bernd

visionmill
Posts: 6
Joined: Fri Sep 12, 2014 2:32 pm

Re: Tab delimitated text import breaks if there is a comma

Post by visionmill » Tue Sep 30, 2014 11:13 pm

Thank everyone. Bernd your solution works great!

phaworth
Posts: 592
Joined: Thu Jun 11, 2009 9:51 pm

Re: Tab delimitated text import breaks if there is a comma

Post by phaworth » Wed Oct 01, 2014 8:13 pm

If I were you I'd get a copy of Alex Tweedly's csv file import handler. It's fast and handles all the oddball situations.
Pete

Post Reply