Hi, Bo...

Not to be a complainer ;-)  but the venerable  new-csv.r  also
shows what appears to be a bug in  parse  (or at least a discrepancy
between documented and actual behavior...)

Consider the "csv" file below, created by a Well-Known Spreadsheet
from a Large Redmond Company:

    1,1/1/00,"""Moo!"" the cow uttered."
    2,1/2/00,"He said ""That's no lady! That's my podiatrist!"""
    3,1/3/00,"This ""bad"" data brought to you by MICROS~1."
    4,1/4/00,"I'm now crying ""Uncle!"""

Notice that the third field of each line is enclosed in quotation
marks and that embedded quotation marks are rendered as *pairs* of
quotation marks.  This can be demonstrated by looking at another
copy of the same file, but saved in ".prn" format -- plain printable
text.

      1    1/1/00  "Moo!" the cow uttered.
      2    1/2/00  He said "That's no lady! That's my podiatrist!"
      3    1/3/00  This "bad" data brought to you by MICROS~1.
      4    1/4/00  I'm now crying "Uncle!"

The problem is that

    parse/all some-string simple-string

is described as breaking  some-string  into a block of sub-strings
delimited by occurrences of  simple-string  (and as ignoring any
"special" significance of characters in  some-string  -- such as
whitespace -- based on the presence of the  /all  refinement.

However, notice that the output of  new-csv.r  is the file:

    ["1" "1/1/00" "" "Moo!" " the cow uttered."]
    ["2" "1/2/00" "He said " "That's no lady! That's my podiatrist!" ""]
    ["3" "1/3/00" "This " "bad" " data brought to you by MICROS~1."]
    ["4" "1/4/00" "I'm now crying " "Uncle!" ""]

The erroneous number of fields per record result indicates that
parse/all  is still special-casing the quotation marks, rather than
just treating them as plain data characters.  As a result of this
glitch in the behavior of  parse/all  new-csv.r  didn't produce the
expected result of

    ["1" "1/1/00" {"Moo!" the cow uttered.}]
    ["2" "1/2/00" {He said "That's no lady! That's my podiatrist!"}]
    ["3" "1/3/00" {This "bad" data brought to you by MICROS~1.}]
    ["4" "1/4/00" {I'm now crying "Uncle!"}]

(or its equivalent) with three fields per record, the most natural
interpretation of the original three-column spreadsheet.

-jn-

[EMAIL PROTECTED] wrote:
> 
> princepawn,
> 
> Attached you will find an example that should allow CSV conversion of a
> file of any length.
> 
> Shocked and honored that simple old %convert-csv.r is one of the most
> popular scripts!
> 
> Have fun!
> 
> -Bo
> 
> On 5-Sep-2000/12:52:36-7:00, [EMAIL PROTECTED] wrote:
> >I was glancing at one of the most popular scripts at the REBOL archive and 
>developed some questions:
> >
> >1- Is it possible to incrementally write to a file in REBOL?
> >2- Is it possible to append to a file in REBOL
> >3- This script seems in-efficient and memory wasteful: it reads and entire file 
>into a temporary array and then converts the array. In Perl, I could do each 
>conversion as I read in each line and write each line out after each conversion.
> >
> >REBOL [
> >    Title:   "Comma-Seperated-Values to REBOL converter"
> >    Date:    16-Jun-1999
> >    Author:  "Bohdan Lechnowsky"
> >    Email:   [EMAIL PROTECTED]
> >    File:    %convert-csv.r
> >    Purpose: <lb>
> >        Convert CSV files to REBOL blocks
> >    <rb>
> >    Category: [file util db text 2]
> >]
> >
> >csv: read/lines to-file ask "Filename to convert from CSV: "
> >
> >block: make block! 100
> >
> >headings: parse/all first csv ","
> >
> >foreach line next csv [append block parse/all line ","]
> >
> >save to-file ask "Filename to save to: " head insert/only block headings
> >
> >
> >Get your FREE Email and Voicemail at Lycos Communications at
> >http://comm.lycos.com
> >
> --
>                            Bohdan "Bo" Lechnowsky
>                            REBOL  Adventure Guide
>            REBOL Technologies 707-467-8000 (http://www.rebol.com)
>                     Download the REBOL Messaging Language
>        The Official Source for REBOL Books (http://www.REBOLpress.com)
> 
>   ------------------------------------------------------------------------
>                 Name: new-csv.r
>    new-csv.r    Type: Plain Text (text/plain)
>             Encoding: base64

Reply via email to