-2

I need split a csv line into variables

# sample data
"100-001","Bla Bla, Bla, Bla","","",0.00000,"W1",6.000,0.000

I tried this but the numerical fields does not have "" so it would not work...

($var0,$var1,$var2,$var3,$var4,$var5,$var6,$var7,$var8) = split(/\","/);

THANKS...

2 Answers2

4

For CSV, you should use a parser. The obvious on is Text::CSV. If as you say, it is difficult to install modules, you can use the Pure Perl implementation, called Text::CSV_PP. Paste that into a file and use it. Problem solved.

The other way to attack this is to use the core module Text::ParseWords which can do a decent CSV parse in a pinch.

#!/usr/bin/env perl

use strict;
use warnings;

use Text::ParseWords;

print "$_\n" for parse_line( ',', 0, '"100-001","Bla Bla, Bla, Bla","","",0.00000,"W1",6.000,0.000');
Joel Berger
  • 20,180
  • 5
  • 49
  • 104
0

This will work:

($var0,$var1,$var2,$var3,$var4,$var5,$var6,$var7,$var8) = 
    split(/(?<="|[0-9]),(?="|[0-9])/);
  • ?<= is a lookbehind assertion
  • ?= is a lookahead assertion
ZnArK
  • 1,533
  • 1
  • 12
  • 23
  • 1
    It **won't** work if you change the first data element to `"100-001,"` (instead of `"100-001"`), and won't work under many other c.s. To parse csv by regex is almost _rocket science_, Friedls [Regex Book](http://books.google.de/books?id=sshKXlr32-AC&lpg=PP1&hl=de&pg=PP1#v=onepage&q&f=false) dedicated at least [one chapter](http://books.google.de/books?id=sshKXlr32-AC&lpg=PP1&hl=de&pg=PA213#v=onepage&q&f=false) to it. – rubber boots Jun 26 '12 at 18:17
  • 1
    @rubberboots That's **correct**. This would violate the rules of a csv field containing commas as the OP has provided. We have to assume that his data set follows some kind of logic. OP does your data have unmatched quotes? – ZnArK Jun 26 '12 at 18:31
  • for the provided input, your regex would, of course, work. Another border case would be _whitespace_ around commas `..." , "...` or `csvtext , csvtext` – rubber boots Jun 26 '12 at 19:00
  • @rubberboots - you're right again, this would have to be customized to his dataset. If it doesn't have much consistency, then writing a regex to fit all scenarios could be quite a challenge. – ZnArK Jun 26 '12 at 19:02
  • I upvoted your post very early on because I guessed (correctly) that you'll probably catch many downvotes later on. The problem here is that your *answer* should be able to instruct a newbie on the topic. But if there are many glitches that you still didn't correct (or at least explain) after being told ... – rubber boots Jun 27 '12 at 17:51
  • @rubberboots I guess I'm new to this, maybe I had some misconceptions about how this whole SO thing was supposed to work. Are we expected to read the OP's mind? I was under the assumption that this site encouraged at least some effort on the part of the original posters. Otherwise this site is going to read: `Hey, I need code that does this....thanks`. The snippet I posted works 100% for the dataset provided. So... Moral of the story is `Post an answer that points the OP in the right direction without exploring every possible scenario not covered in the OP = -1`. I guess I udnerstand now – ZnArK Jun 27 '12 at 18:30
  • I think **SO** is just another multi-user computer game environment as many others are. It's just a modern *text adventure*. Use it for your personal enlightment and try to learn something important from it by playing it hard ;-) – rubber boots Jun 28 '12 at 08:19