|
Pages: [1]
|
 |
|
Author
|
Topic: Parse and Dialects (Read 2124 times)
|
DefiantPc
Administrator
Jr. Member
Offline
Posts: 70
|
Discussions concerning Parse and Dialects.
|
|
|
|
|
Logged
|
|
|
|
|
-jn-
|
How does one read this discussion? The header says that there are 31 responses, but I can't see any of them!
|
|
|
|
|
Logged
|
|
|
|
|
Amanita
|
There are 31 views not posts, but you are welcome to start your own topics.
|
|
|
|
|
Logged
|
|
|
|
yos
Guest
|
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
|
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'. "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
|
Would PARSE/ALL work with a check for commas? ie... >> 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
|
Well, it seems you want to parse CSV data  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  Then it can give something like that : 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 
|
|
|
|
|
Logged
|
|
|
|
|
Gord
|
Thanks DideC.  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
|
Hum, I did not test it on the sample you find on the site. And it does not work very well on it  So you have find a bug in the script ;-)
|
|
|
|
|
Logged
|
|
|
|
|
|
Pages: [1]
|
|
|
 |
News: 01-09-08 Alpha version of REBOL 3 has been released!
2251 Posts in 589 Topics by 2420 Members
Latest Member: pharmacytovvv
|