Use a CSV parsing library, rather than rolling your own.  They take
care of all that stuff for you.  I've used
http://ostermiller.org/utils/CSV.html in the past.

If you really want to parse it yourself, you can use listToArray, and
then iterate over the array and combine items that are quoted.  For
example take this line:

1,"barney","boisvert, \"crazy man\", barney","1234 Main Street, Apt 5"

When you listToArray it, you'll get this:

[
 '1',
 '"barney"',
 '"boisvert',
 ' \"crazy man\"',
 ' barney"',
 '"1234 Main Street',
 ' Apt 5"'
]

Note the position of the double quotes in the items.  What you need to
do is find items that START with a double quote, and then combine them
with the following elements until you find an element that ENDS with a
double quote.  You'll need to handle the case when the current item is
the whole string (in the case of the second element), and when the
ending quote is escaped (the forth item).

In this particular case you need to remove the quotes from item 2
(since it's a whole string to itself), and you need to combine items
3, 4 and 5 (3 starts with a quote, 5 ends with a quote).  The quotes
in item 4 are escaped, so they should be ignored for combination, and
then the backslashes should be removed after the fact.  Note that
you'll need to deal with double-escaping.  As you can see, it's a
mess, so I'd highly recommend the third-party library.  ;)

cheers,
barneyb

On Tue, Dec 22, 2009 at 8:51 AM, Phillip Vector
<vec...@mostdeadlygame.com> wrote:
>
> I have a CSV that looks like the following...
>
> 3270,5101650,"Dewey, Cheatum & Howe ", 0   , 0   ,0.00,0.00,9.25,-9.25
> 3270,5101650,Phillip Vector                , 34 ," 3,161.00
> ",92.97,79.25,61.76,17.50
> 3270,5101650,"James P. Kardone JR., P.C.     ", 0   , 0   
> ,0.00,0.00,9.25,-9.25
>
> I'm stuck on how to process the fields and load them into an array.
> How would I say, "If the field is surrounded by quotes, then ignore
> the commas in it"?
>
> I would think I would need to load it into the array as a field before
> I could find out if it's surrounded by quotes. But I can't load it
> correctly into the array until I find out if there are quotes around
> it.
>
> Changing the CSV is not an option (I know, it would make things much easier).
>
> Ideas?
>
> 

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~|
Want to reach the ColdFusion community with something they want? Let them know 
on the House of Fusion mailing lists
Archive: 
http://www.houseoffusion.com/groups/cf-talk/message.cfm/messageid:329313
Subscription: http://www.houseoffusion.com/groups/cf-talk/subscribe.cfm
Unsubscribe: http://www.houseoffusion.com/cf_lists/unsubscribe.cfm?user=89.70.4

Reply via email to