From robert.l.harris at gmail.com Thu Oct 3 15:31:45 2013 From: robert.l.harris at gmail.com (Robert L. Harris) Date: Thu, 3 Oct 2013 16:31:45 -0600 Subject: [Denver-pm] Splitting Data Message-ID: $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 a 1 in 10000 chance it'll break but accuracy counts. -- :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: From larryl at emailplus.org Thu Oct 3 15:42:31 2013 From: larryl at emailplus.org (Larry Leszczynski) Date: Thu, 03 Oct 2013 16:42:31 -0600 Subject: [Denver-pm] Splitting Data In-Reply-To: References: Message-ID: <1380840151.2471.29758001.28E41CBD@webmail.messagingengine.com> 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 From David.Olson2 at Honeywell.com Thu Oct 3 15:45:49 2013 From: David.Olson2 at Honeywell.com (Olson, David) Date: Thu, 3 Oct 2013 22:45:49 +0000 Subject: [Denver-pm] Splitting Data In-Reply-To: References: Message-ID: Dear Robert, My first guess would be that $String isn't really what you expect. Try escaping the double quotes as in $String = 'component1,component2,\"This is my, test string\", component4'; David Olson Reliability Engineer Reliability & Maintainability Engineering Honeywell, Inc. REQUIRED NOTICE: Please note that I am NOT authorized to negotiate, bind or make commitments on behalf of Honeywell Technology Solutions, Inc. (HTSI). No email or conversation with me should be viewed as providing direction for which compensation can be expected. Any forthcoming formal agreement will come from HTSI's Contracts and Procurement Organization. This communication, including any attachment, contains information that may be confidential or privileged, and is intended solely for the entity or individual to whom it is addressed. If you are not the intended recipient, please notify the sender at once, and you should delete this message and are hereby notified that any disclosure, copying, or distribution of this message is strictly prohibited. From: Denver-pm [mailto:denver-pm-bounces+david.olson2=honeywell.com at pm.org] On Behalf Of Robert L. Harris Sent: Thursday, October 03, 2013 4:32 PM To: Denver-pm at pm.org Subject: [Denver-pm] Splitting Data $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 a 1 in 10000 chance it'll break but accuracy counts. [https://mail.google.com/mail/u/0/images/cleardot.gif] -- :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: From robert.l.harris at gmail.com Thu Oct 3 16:03:12 2013 From: robert.l.harris at gmail.com (Robert L. Harris) Date: Thu, 3 Oct 2013 17:03:12 -0600 Subject: [Denver-pm] Splitting Data In-Reply-To: <1380840151.2471.29758001.28E41CBD@webmail.messagingengine.com> References: <1380840151.2471.29758001.28E41CBD@webmail.messagingengine.com> Message-ID: That worked perfectly, Thank you. On Thu, Oct 3, 2013 at 4:42 PM, Larry Leszczynski 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: