[Denver-pm] Splitting Data

Robert L. Harris robert.l.harris at gmail.com
Thu Oct 3 16:03:12 PDT 2013


That worked perfectly,  Thank you.


On Thu, Oct 3, 2013 at 4:42 PM, Larry Leszczynski <larryl at emailplus.org>wrote:

> Hi Robert -
>
> > $String='component1,component2,"This is my, test string", component4';
> >
> > ($C1, $C2, $Str, $C4) = split(',', $String);
> >
> > I'm only getting "This is my" in $Str and $C4 does not contain
> > "component4".  Is there a graceful way of handling this?
>
> It's doing what you asked, namely splitting on any comma it finds - it
> does not know that
> you do not want it to split in the middle of a double-quoted string...
>
> That line looks like a line you would get in a CSV file, so I would
> handle it that way:
>
>     use Text::CSV;
>
>     my $line = 'component1,component2,"This is my, test string",
>     component4';
>
>     my $csv = Text::CSV->new;
>
>     $csv->parse($line) or die $csv->error_diag();
>
>     my @columns = $csv->fields();
>
> At this point, @columns contains:
>
>     $columns[0]:  'component1'
>     $columns[1]:  'component2'
>     $columns[2]:  'This is my, test string'
>     $columns[3]:  ' component4'
>
> HTH,
> Larry
> _______________________________________________
> Denver-pm mailing list
> Denver-pm at pm.org
> http://mail.pm.org/mailman/listinfo/denver-pm
>



-- 
:wq!
---------------------------------------------------------------------------
Robert L. Harris

DISCLAIMER:
      These are MY OPINIONS             With Dreams To Be A King,
       ALONE.  I speak for                      First One Should Be A Man
       no-one else.                                     - Manowar
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.pm.org/pipermail/denver-pm/attachments/20131003/6ecc7737/attachment-0001.html>


More information about the Denver-pm mailing list