Read a text file

Alex Tweedly alex at tweedly.net
Mon Jan 17 17:00:05 CST 2005


Frank D. Engel, Jr. wrote:

> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
>
> hmm, minor improvements (and to kill off the quotes):
>
> put URL "file:Stories/Westwind" into vArr
> repeat for each line x in vArr   -- faster than with i= notation
>   -- separate fields
>   put item 1 of x into vName
>   put item 2 of x into vID
>   put item 3 of x into vMail
>
>   -- delete quotes
>   delete char 1 of vName
>   delete char 1 of vID
>   delete char 1 of vMail
>   delete the last char of vName
>   delete the last char of vID
>   delete the last char of vMail
>
>   -- place into fields: note this will only result in the last line of 
> the file being in the fields, since
>   -- it replaces the former contents each time.  modify as needed to 
> fix this
>
>   put vName into field "Name"
>   put vID into field "ID"
>   put vMail into field "Email address"
> end repeat


Well, sigh .....  This brings us back to the "fun" of parsing CSV 
files.  See some long thread(s) from a few months ago.

Given an input line like
"Tweedly, Alex", "id1","alex at tweedly.net"
this script, like the other ones given in this thread, will give a 
result that might be a surprise.

The "obviously correct" answer that many would expect is

> name = Tweedly, Alex
> id = id1
> email = alex at tweedly.net

but this scripts would get

> name = Tweedl
> id = lex
> email = id1


The quotes in the input file do NOT hide the commas, and so every comma 
would be an item delimiter.

This may not be an issue in this particular case, depending on what 
constraints Paul knows of in the input data. If that possibility needs 
to be accounted for, you need to either parse char by char, or use the 
fast but rather opaque "split and alternate" method from one of the 
earlier threads, at
  http://lists.runrev.com/pipermail/use-revolution/2004-October/045450.html

Also, there are some email addresses that can ONLY be properly 
represented by including quotes because they contain commas. For instance
   "any name you might, perhaps, like" <alex at tweedly.net>
is a valid email address.
This form of valid email address might suffer from trying to store in 
this format, or at least might need to choose one or other of the 
various ways CSV files handle included quotes (either doubling them 
within the field, or escaping them with something like '\').

-- Alex.




-- 
No virus found in this outgoing message.
Checked by AVG Anti-Virus.
Version: 7.0.300 / Virus Database: 265.6.11 - Release Date: 12/01/2005



More information about the use-livecode mailing list