How to use quotewords for CSV text, but still make it not ignore the trailing empty fields?

Discussion in 'Perl Misc' started by mmasroorali@cse.buet.ac.bd, May 2, 2005.

  1. Guest

    Dear All,
    I have some autogenrated CSV text where a typical line looks like this,

    "A", "B, C and D", "E",,,,,

    See the empty fields at the end? Now when I use quotewords, the
    trailing null fields are stripped from the result. So, I get only three
    fields. But I want to get the last columns as empty fields. In case of
    split, I could use the limit as the third parameter. Could any kind
    soul please tell me what I could do in this situation?

    Regards,
    , May 2, 2005
    #1
    1. Advertising

  2. wrote in
    news::

    > Dear All,
    > I have some autogenrated CSV text where a typical line looks like
    > this,
    >
    > "A", "B, C and D", "E",,,,,
    >
    > See the empty fields at the end? Now when I use quotewords, the
    > trailing null fields are stripped from the result.


    Please read the posting guidelines for this group. Please always post a
    minimal example that exhibits the problem you want to solve.

    > So, I get only three fields.


    How so?

    use strict;
    use warnings;

    use Data::Dumper;

    use Text::parseWords;

    my @fields = quotewords(',\s*', 0, q{"A", "B, C and D", "E",,,,,});
    print Dumper \@fields;

    __END__

    C:\Home> c.pl

    $VAR1 = [
    'A',
    'B, C and D',
    'E',
    '',
    '',
    '',
    '',
    undef
    ];

    I don't know why the last field is undef rather than ''.

    Sinan
    --
    A. Sinan Unur <>
    (reverse each component and remove .invalid for email address)

    comp.lang.perl.misc guidelines on the WWW:
    http://mail.augustmail.com/~tadmc/clpmisc/clpmisc_guidelines.html
    A. Sinan Unur, May 2, 2005
    #2
    1. Advertising

  3. Damian James Guest

    On 1 May 2005 20:15:19 -0700, said:
    > Dear All,
    > I have some autogenrated CSV text where a typical line looks like this,
    >
    > "A", "B, C and D", "E",,,,,
    >
    > See the empty fields at the end? Now when I use quotewords, the
    > trailing null fields are stripped from the result. So, I get only three
    > fields. But I want to get the last columns as empty fields. In case of
    > split, I could use the limit as the third parameter. Could any kind
    > soul please tell me what I could do in this situation?
    >


    Quotewords? You don't mean as in 'qw'? Don't see how that applies.

    Use Text::CSV. Really, there are more issues that dealing with additional
    commas. You don't want to revisit them all.

    --damian


    --
    @:=grep!(m!$/|#!..$|),split//,<DATA>;@;=0..$#:;while($:=@;){$;=rand
    $:--,@;[$;,$:]=@;[$:,$;]while$:;push@|,shift@;if$;[0]==@|;select$,,
    $,,$,,1/80;print qq x\bxx((@;+@|)*$|++),@:[@|,@;],!@;&&$/} __END__
    Just another Perl Hacker,### http://home.pacific.net.au/~djames.hub
    Damian James, May 4, 2005
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. John B. Matthews
    Replies:
    4
    Views:
    666
    John B. Matthews
    Sep 12, 2008
  2. Roedy Green
    Replies:
    3
    Views:
    620
  3. sso
    Replies:
    20
    Views:
    2,655
    Martin Gregorie
    Apr 26, 2009
  4. johkar
    Replies:
    2
    Views:
    2,880
    Mayeul
    Dec 10, 2009
  5. Rob Meade
    Replies:
    6
    Views:
    264
    Rob Meade
    Mar 1, 2004
Loading...

Share This Page