Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Re: Reading escaped data from a CSV

by hanenkamp (Pilgrim)
on Nov 14, 2003 at 22:40 UTC ( [id://307232]=note: print w/replies, xml ) Need Help??


in reply to Reading escaped data from a CSV

First my rant: No offense monks, but some of the answers I've read to this question are kind of pathetic. I don't care whether or not there is a module or not, but sometimes it is helpful to know the technique. Sheesh. Feel free to make comments about using modules, but at least give him/her some direction besides, "No one does this anymore, just use the module or forget it!" Reinventing the wheel is a time-honored process for learning. Nice way to be rude to a potential JAPH.

Anonymous, my first suggestion is to browse CPAN for the modules that have been mentioned already by others. At http://search.cpan.org/ you can view the documentation of modules and then click on the "Source" links to view the actual source. Then, you can see how the module author has done it.

If you are interested in really doing this yourself, which I, like the others, would not recommend. You can use a negative lookup in a split to use escaped commas:

my @fields = split /(?<!\\),/, $line;

Another solution is to use a character which you do not allow in your strings, which is why characters like ^ or ~ or | are often used as delimiters in CSV files.

The most common solution, however, is to use quotes around strings to escape an entire string. That is, commas are only available for splitting outside of quotes. This is difficult enough that I don't, off-hand, have a simple solution, though I've seen a few...for this, I would definitely suggest browsing the source of a pertinent module.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://307232]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others studying the Monastery: (3)
As of 2024-04-19 05:17 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found