Rebol Talk Forum  |  REBOL Discussions  |  REBOL Core  |  Topic: Parse and Dialects
Pages: [1] Print
Author Topic: Parse and Dialects  (Read 2002 times)
DefiantPc
Administrator
Jr. Member
*
Offline Offline

Posts: 70


View Profile
Parse and Dialects
« on: January 13, 2004, 09:36:01 AM »

Discussions concerning Parse and Dialects.
Logged
-jn-
Newbie
*
Offline Offline

Posts: 4


View Profile
Parse and Dialects
« Reply #1 on: January 26, 2004, 11:40:29 AM »

How does one read this discussion?  The header says that there are 31 responses, but I can't see any of them!
Logged
Amanita
Newbie
*
Offline Offline

Posts: 19


View Profile
Parse and Dialects
« Reply #2 on: January 26, 2004, 12:04:32 PM »

There are 31 views not posts, but you are welcome to start your own topics.  Smiley  
Logged
yos
Guest


Email
Parse and Dialects
« Reply #3 on: January 28, 2004, 03:45:48 AM »

For the moment the only things i do with parse is to get a string date with
the format the user want :

replace sys.r by system or type in console
sys.r: get in system 'self
before doing this :

REBOL []

date: get in context
[   date: func
    [   {
        }
        /with Rwith
        /to Rto
        /local
            two-digits result d chars
    ]
    [   two-digits: func [d] [copy back back tail insert to-string d "0"]
        Rwith: any [Rwith [day-name " " day " " month-name " " year]]
        result: copy ""
        d: any [Rto  now/date]
        parse Rwith
        [   any
            [   set chars string! (append result chars) |
                set chars char! (append result chars) |
                'year (append result to-string d/year) |
                'month (append result two-digits d/month) |
                'day (append result two-digits d/day) |
                'weekday (append result to-string d/weekday) |
                'day-name (append result pick sys.r/locale/days d/weekday) |
                'month-name (append result pick sys.r/locale/months d/month)
            ]
        ]
        result
    ]
] 'date

probe date
probe date/with [month-name " " year]
probe date/with [year month day]


It could be a good exercice to code the C printf commande with the
caractère replacements %d %s etc ...

 
Logged
Gord
Newbie
*
Offline Offline

Posts: 18


View Profile
Re: Parse challenge
« Reply #4 on: September 29, 2006, 01:54:23 PM »

It seems I don't understand how to parse a file based on lines (newline). Each line of data contains a variable number of quoted strings and the quoted string can contain carriage returns.  Here is an example text file we'll refer to as 'Data'.
Code:
"Hello
World","0","MoreText", ...
{line two of quoted strings}
What I want:
blocks of items that will contain the quoted strings (one or more quoted strings per line of text). Ex:
["Hello ^M^/ World","0","More Text",...]
[{another line of quoted strings}]
...
What I get:
Either:
1. A single string of characters - not split up into blocks at all (Data: read TextFile.txt)
2. Strings split up at every newline even if they are part of a quoted string. (Data: read/lines textfile.txt)
3. Strings split up at every comma but not grouped with other strings on the same line.  (The challenge is that we don't know how many quoted strings will be on each line of data.  We only know that the group of quoted strings will end in a 'Newline' instead of a comma)

Maybe, parsing the file is not the solution to getting the lines of quoted strings into a block.  I've also tried a few other commands like 'mold', but I'm not hitting the right combination at this point.

« Last Edit: September 29, 2006, 05:38:15 PM by Gord » Logged
CarlRead
Full Member
***
Offline Offline

Posts: 105


View Profile
Re: Parse and Dialects
« Reply #5 on: September 30, 2006, 03:12:54 AM »

Would PARSE/ALL work with a check for commas?  ie...
Code:
>> text: {"Hello^/World","0","MoreText","Line with^/carriage return","etc..."}
== {"Hello
World","0","MoreText","Line with
carriage return","etc..."}
>> print text
"Hello
World","0","MoreText","Line with
carriage return","etc..."
>> parse/all text ","
== ["Hello^/World" "0" "MoreText" "Line with^/carriage return" "etc..."]
Logged

- Carl Read
DideC
Newbie
*
Offline Offline

Posts: 26


View Profile
Re: Parse and Dialects
« Reply #6 on: September 30, 2006, 04:36:27 AM »

Well, it seems you want to parse CSV data  Wink

CSV looks easy to parse, but the tricky part is that quoted values can contains newlines. Then 'read/lines goes useless.
But more, quoted values (CSV string in fact) can contain quotes (in fact I speak of double quote, like this ").

Then to use 'parse you need to see a CSV files by it's semantic aspect.

A CSV file is made of an header and some records separated by newlines.
Each of them are made of values separated by a separator (generally it's a ",").
And a value, can be just some chars with some spaces before of after.
But a value can also be surrounded by double quotes. In this case, the value can contain newlines or even double quotes but then there are doubled.
And consider that newlines can be just CR or LFCR and that some programs use both to distinguish newlines between records and newlines in values (Excel does AFAIK).

Then you realise that CSV data is not so simple to read as it looks like at the beginning  Undecided
Then it can give something like that :

Code:
csv-to-block: func [
"Convert a string of CSV formated data to a Rebol block. First line is header."
csv-data [string!] "CSV data."
/separator separ [char!] "Separator to use if different of comma (,)."
/without-header "Do not include header in the result."
/local out line start end this-string header record value data chars spaces chars-but-space
; CSV format information http://www.creativyst.com/Doc/Articles/CSV/CSV01.htm
] [
out: copy []
separ: any [separ #","]

; This function handle replacement of dual double-quote by quote while copying substring
this-string: func [s e] [replace/all copy/part s e {""} {"}]
; CSV parsing rules
header: [(line: copy []) value any [separ value] (if not without-header [append/only out line])]
record: [(line: copy []) value any [separ value] (append/only out line)]
value: [any spaces data any spaces (append line this-string start end)]
data: [start: some chars-but-space end: | #"^"" start: any [some chars | {""} | #"," | newline] end: #"^""]
chars: complement charset rejoin [ {"} separ newline]
spaces: charset exclude { ^-} form separ
chars-but-space: exclude chars spaces

parse/all csv-data [header any [newline record] any newline end]
out
]

probe csv-data: csv-to-block read %my-file.csv

In the header you will find a link to a page that explain well the CSV format  Cheesy
Logged
Gord
Newbie
*
Offline Offline

Posts: 18


View Profile
Re: Parse and Dialects
« Reply #7 on: October 02, 2006, 05:51:29 PM »

Thanks DideC.   Smiley  For some reason that I haven't figured out yet, your CVS script works well except that it will stop on empty fields.  (IE: a line that starts with a comma or two commas in a row).  I see that your sample data on the web site link has an empty field (two commas in a row), so I will have to see what is different about my data.  For now I just loaded the data into an editor and replaced any {,,} with {,"",} and that made the CVS script work again.
Logged
DideC
Newbie
*
Offline Offline

Posts: 26


View Profile
Re: Parse and Dialects
« Reply #8 on: October 04, 2006, 04:40:35 PM »

Hum, I did not test it on the sample you find on the site.
And it does not work very well on it Embarrassed

So you have find a bug in the script ;-)
Logged
Pages: [1] Print 
Rebol Talk Forum  |  REBOL Discussions  |  REBOL Core  |  Topic: Parse and Dialects
Jump to:  

  
Quick Search...

Advanced search
  
Welcome, Guest. Please login or register.
Did you miss your activation email?
August 30, 2008, 07:02:11 AM
Username: Password: Session Length:
  

News: 01-09-08

Alpha version of REBOL 3 has been released!


  
2235 Posts in 583 Topics by 1757 Members
Latest Member: Xzycdysz

  Rebol Talk Forum | Powered by SMF 1.0.9.
© 2001-2005, Lewis Media. All Rights Reserved.

RT design by Defiant Pc