Sorry, brain cramp.  Here is the test code followed by the actual code.

Test script --

<cfset lstCSV="">
<cfinclude template="includes\parsecsv.cfm">
<cfoutput><br>[#lstCSV#]<br>Nothing Should show</cfoutput>
<cfdump var="#aryCSV#">

<cfset lstCSV=",">
<cfinclude template="includes\parsecsv.cfm">
<cfoutput><br>[#lstCSV#]<br>2 blank Should show</cfoutput>
<cfdump var="#aryCSV#">

<cfset lstCSV=",,">
<cfinclude template="includes\parsecsv.cfm">
<cfoutput><br>[#lstCSV#]<br>3 blank Should show</cfoutput>
<cfdump var="#aryCSV#">

<cfset lstCSV="Test">
<cfinclude template="includes\parsecsv.cfm">
<cfoutput><br>[#lstCSV#]<br>1 Should show</cfoutput>
<cfdump var="#aryCSV#">

<cfset lstCSV='Test,", Bomb",,'>
<cfinclude template="includes\parsecsv.cfm">
<cfoutput><br>[#lstCSV#]<br>4 should show, last 2 blank</cfoutput>
<cfdump var="#aryCSV#">

<cfset lstCSV="0,1,2,3,4">
<cfinclude template="includes\parsecsv.cfm">
<cfoutput><br>[#lstCSV#]<br>5 Should show</cfoutput>
<cfdump var="#aryCSV#">

<cfset lstCSV='a, "Testing this as ""an"", option", testing this'>
<cfinclude template="includes\parsecsv.cfm">
<cfoutput><br>[#lstCSV#]<br>3 Should show</cfoutput>
<cfdump var="#aryCSV#">

<cfset lstCSV='123, a, "Testing this as ""an"", option"'>
<cfinclude template="includes\parsecsv.cfm">
<cfoutput><br>[#lstCSV#]<br>3Should show</cfoutput>
<cfdump var="#aryCSV#">

<cfset lstCSV='123, "Testing ending "quotes"", 3'>
<cfinclude template="includes\parsecsv.cfm">
<cfoutput><br>[#lstCSV#]<br>3 Should show</cfoutput>
<cfdump var="#aryCSV#">

<cfset lstCSV="123, a,, option a,">
<cfinclude template="includes\parsecsv.cfm">
<cfoutput><br>[#lstCSV#]<br>5 Should show, 3 & 5 blank</cfoutput>
<cfdump var="#aryCSV#">

<cfset lstCSV='123,"Testing, Inc",This'>
<cfinclude template="includes\parsecsv.cfm">
<cfoutput><br>[#lstCSV#]<br>3 Should show</cfoutput>
<cfdump var="#aryCSV#">

<cfset lstCSV='Sutton Place Hay Day,308316,1/13/03,Hay Day,1050 East Putnam
Avenue,,Greenwich,CT,06830,,538195,UPS
Ground,1/13/03,6,24.00,12851gr,00613,1,"Double Hearts,   with I LOVE
YOU",2.20,-52.80'>
<cfinclude template="includes\parsecsv.cfm">
<cfoutput><br>[#lstCSV#]<br>21 Should show</cfoutput>
<cfdump var="#aryCSV#">

<cfset lstCSV='"Andersen, John","Mr. John A. Andersen, Jr.",John Andersen'>
<cfinclude template="includes\parsecsv.cfm">
<cfoutput><br>[#lstCSV#]<br>3 Should show</cfoutput>
<cfdump var="#aryCSV#">



Parsing program

<!---
Object:         ParseCSV

Purpose:        Include to parse a CSV string named lstCSV and return in array
named aryCSV

Author:         Andy Ousterhout(ACO)

Date:           3/3/2002

Version:        V1.0

Usage:  CSV files have fields in a record separated by commas, ",".  However,
if a text field contains an embededd comma, the entire field is placed in
double quotes.  For example,

        00007,"Bear, Swimming",Test
        00008,"Frank ""The Animal"" Hooter", testing
        00009, Testing, "Frank ""The Animal"" Hooter"

        Should each result in 3 fields being parsed.


Change Record:
Who:            When:           Why:


 --->

        <cfscript>
                aryCSV = ArrayNew(1);

                // Only do stuff if something passed ....
                if (len(lstCSV) GT 0) {

                        // First, add a comma onto the end so that the search for ", 
and not "",
always finds something unless bad CSV string.
                        // Then, replace any ",," with ", ," so that individual array 
elements
will be found

                        lstWork = Trim(lstCSV) & ",";
                        lstWork = replace(lstWork, ",", ", ", "ALL");

                        // Then loop thru string, parsing off to next comma and 
checking if
complete field
                        // Note that if the field has imbedded quotes, this routine 
will create
bad data.
                        // For example, [This is a test, "test"] will come through as 
["This is a
test, ""test""]
                        // and will be parsed into two fields, [This is a test, "] and 
["test"].
The calling routine will
                        // need to handle the possibility that more fields will be 
returned then
expected.

                        Do {

                                // If Line starts with quote, next field is next 
quote+comma that is not
part of a quote+quote+comma

                                if (left(lstWork, 1) EQ '"') {
                                        tmpStr = Right(lstWork, len(lstWork)-1);
                                        EndPos = REFInd('([^"]",) | ("*"",)', tmpStr);

                                        // If nothing found, look for "", ending
                                        if (EndPos EQ 0) {
                                                EndPos=REFInd('([^"]",) | 
(["]*["",])', tmpStr);
                                                EndPos=REFInd('("",)', Mid(tmpStr, 
EndPos+1, len(tmpStr)))+EndPos;
                                        }

                                        // If still nothing found, Error.  Stop work 
immediately
                                        if (EndPos EQ 0) {
                                                lstwork = "";
                                                NextField = "";
                                        }
                                        Else {
                                                NextField = mid(trim(lstWork), 2, 
EndPos);
                                                if (len(lstWork)-(EndPos+2) EQ 0)
                                                        lstwork = "";
                                                else
                                                        lstWork = ltrim(right(lstWork, 
len(lstWork)-(EndPos+3)));
                                        }
                                }
                                else if (left(lstwork, 1) EQ ",") {
                                        lstWork = ltrim(right(lstWork, 
len(lstWork)-1));
                                        NextField = " ";
                                }
                                else {
                                        NextField = Trim(ListFirst(lstWork, ","));
                                        lstWork = LTrim(ListDeleteAt(lstWork, 1, ","));
                                }

                                //Append the new field, converting CSV's double quotes 
to single quotes

                                ArrayAppend(aryCSV, Replace(NextField, '""', '"', 
"all"));
//writeoutput(",[#lstwork#], [#NextField#]");
                        }
                        while (len(lstWork) GT 1);
                }
        </cfscript>




-----Original Message-----
From: Robertson-Ravo, Neil (RX)
[mailto:[EMAIL PROTECTED]]
Sent: Thursday, January 30, 2003 8:49 AM
To: CF-Talk
Subject: RE: New problem with csv... argghhh


attachments are stripped.....send it inline.

-----Original Message-----
From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED]]
Sent: 30 January 2003 14:45
To: CF-Talk
Subject: RE: New problem with csv... argghhh


At 08:38 AM 1/30/03 -0600, you wrote:
>I've attached what I use to parse my CSV's into an array, which I can then


Andy - no attachment!



~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~|
Archives: http://www.houseoffusion.com/cf_lists/index.cfm?forumid=4
Subscription: 
http://www.houseoffusion.com/cf_lists/index.cfm?method=subscribe&forumid=4
FAQ: http://www.thenetprofits.co.uk/coldfusion/faq
Signup for the Fusion Authority news alert and keep up with the latest news in 
ColdFusion and related topics. http://www.fusionauthority.com/signup.cfm

                                Unsubscribe: 
http://www.houseoffusion.com/cf_lists/unsubscribe.cfm?user=89.70.4
                                

Reply via email to