[REBOL] Re: csv file reading
From: christian::ensel::gmx::de at: 31-Jan-2008 23:01
Hi John,
/first/, despite not knowing the origin and format restrictions of the
emails you have to deal with, I suspect that commas probably aren't the
perfect choice to delimit the data fields. Just think of an email
including a comma, be it one single line or not, wouldn't that be
interpreted as two fields, anyway?
/Second/, regarding multi-line emails, maybe { and } can be used:
{Customer} {server} {contact} {phone} {this is
a longer comment with
some
line-breaks.}
But, depending on the software it isn't too likely that you can have
different opening and closing separators as in { and }, so some other
approach may be needed.
For starters, so, have a look at the following IMPORT-TICKETS function:
------------------------------------------------------------------------
import-tickets: func [
{Imports tickets from raw data string.
Returns a block containing a bolean value indicating whether all raw
data has been parsed successfully
and a block of the tickets imported (may be empty and gets cleared with
every function call).}
raw [string!] "Raw string data to be imported"
/with "Delimiters to be used"
field-delimiter [char!] "delimiter between fields (defaults to %)"
ticket-delimiter [char!] "delimiter between fields (defaults to %,
too)"
/local tickets
] [
field-delimiter: any [field-delimiter #"%"]
ticket-delimiter: any [ticket-delimiter #"%"]
clear tickets: []
reduce [
parse/all raw [
some [
copy name to field-delimiter 1 skip
copy first-name to field-delimiter 1 skip
copy email to field-delimiter 1 skip
copy phone to field-delimiter 1 skip
[ copy comment to ticket-delimiter 1 skip
| copy comment to end
]
(insert/only tail tickets new-line/all reduce [trim
name trim first-name trim email trim phone trim comment] on)
]
]
new-line/all tickets on
]
]
------------------------------------------------------------------------
USAGE:
IMPORT-TICKETS raw /with field-delimiter ticket-delimiter
DESCRIPTION:
Imports tickets from raw data string.
Returns a block containing a bolean value indicating whether all raw
data has been parsed successfully
and a block of the tickets imported (may be empty and gets cleared with
every function call).
IMPORT-TICKETS is a function value.
ARGUMENTS:
raw -- Raw string data to be imported (Type: string)
REFINEMENTS:
/with -- Delimiters to be used
field-delimiter -- delimiter between fields (defaults to %)
(Type: char)
ticket-delimiter -- delimiter between fields (defaults to %,
too) (Type: char)
------------------------------------------------------------------------
probe import-tickets {Bill%Bilbo%bill-yahoo.com%555-555-5555%Please open
a ticket for this
server
in
line
4%
Bill%Bilbo%bill-yahoo.com%555-555-5555%Please open the ticket now!%
Random%Joe%joe-random.com%123-456-7890%Yet another
multi-line
comment}
------------------------------------------------------------------------
[true [
[
"Bill"
"Bilbo"
"bill-yahoo.com"
"555-555-5555"
{Please open a ticket for this
server
in
line
4}
]
[
"Bill"
"Bilbo"
"bill-yahoo.com"
"555-555-5555"
"Please open the ticket now!"
]
[
"Random"
"Joe"
"joe-random.com"
"123-456-7890"
"Yet another^/multi-line^/comment"
]
]]
------------------------------------------------------------------------
HTH,
Christian
John Blake schrieb: