Re: Reading a file into a "sequence"
- Posted by "Unkmar" <L3Euphoria at bellsouth.net> Nov 18, 2004
- 557 views
----- Original Message ----- From: "Derek Parnell" <guest at RapidEuphoria.com> To: <EUforum at topica.com> Sent: Wednesday, November 17, 2004 9:03 PM Subject: Re: Reading a file into a "sequence" > > > posted by: Derek Parnell <ddparnell at bigpond.com> > > John F Dutcher wrote: >> >> I thought I'd get the find on the inappropriate reference [1..4] before >> anyone answered...but you're too fast for me....Thanks > > Here is a function to convert a Comma-Separated-Value string to > a sequence of fields. It handles escaped characters via the '\' > character and embedded quotes can also be represented by a twin > quotes. > > }}} <eucode> > function CSV_to_Sequence(object pText) > sequence lResult > integer lInField > integer lFld > integer lPos > > lResult = {} > if atom(pText) then > return lResult > end if > > if length(pText) != 0 then > if pText[length(pText)] = '\n' then > pText = pText[1..length(pText)-1] > end if > end if > > lInField = 0 > lFld = 0 > lPos = 1 > while lPos <= length(pText) do > if pText[lPos] = '"' then > if lInField then > if lPos != length(pText) then > if pText[lPos+1] = '"' then > -- First quote of a Double quote found. > lResult[lFld] &= '"' > lPos += 1 > else > lInField = 0 > end if > else > -- End of field found. > lInField = 0 > end if > else > -- Start of field found > lInField = 1 > lResult = append(lResult, {}) > lFld = length(lResult) > end if > elsif pText[lPos] = '\\' then > -- Found the 'escape' lead in character > if lInField then > if lPos != length(pText) then > lPos += 1 > if pText[lPos] = 'n' then --New Line > lResult[lFld] &= 10 > elsif pText[lPos] = 't' then --Tab > lResult[lFld] &= 9 > elsif pText[lPos] = 'r' then --Carriage Return > lResult[lFld] &= 13 > elsif pText[lPos] = '\\' then -- Back Slash > lResult[lFld] &= '\\' > elsif pText[lPos] = 's' then -- Space > lResult[lFld] &= ' ' > else > lResult[lFld] &= pText[lPos] > end if > end if > else > -- ignore it > end if > else > if lInField then > -- add it to current field data > lResult[lFld] &= pText[lPos] > else > -- do nothing as I'm not in a field. > end if > end if > lPos += 1 > end while > > return lResult > end function > </eucode> {{{ > > Use it like this ... > > sequence Fields > > Fields = CSV_to_Sequence( gets(fh) ) > > -- > Derek Parnell > Melbourne, Australia I created a csv lib a long time back. but lets all completely forget about that. It is never used so why bother remembering that it even exists. unkmar