Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Re: split problem

by codiac (Beadle)
on Oct 03, 2014 at 10:19 UTC ( #1102714=note: print w/replies, xml ) Need Help??


in reply to split problem

Why use regex when there is a module to do this for you?
#!/usr/bin/perl use strict; use warnings; use Text::CSV; my $csv = Text::CSV->new ({ sep_char => '|' }); while (<DATA>){ my $line = $_; chomp; next if (/Order/); print ("string $_ \n"); $_ =~ s/\"//g; my @array = split /\|/ ; print("order $array[0] , wrong sometimes $array[3]\n"); my $status = $csv->parse($line); my @columns = $csv->fields(); print("order $columns[0] , right $columns[3]\n\n"); } __DATA__ "129822"|"Custom Currency"|"Living the | Dream"|"400" "129823"|"Custom Currency"|"Living the Dream"|"500"
Outputs:
string "129822"|"Custom Currency"|"Living the | Dream"|"40 order 129822 , wrong sometimes: Dream order 129822 , right 400 string "129823"|"Custom Currency"|"Living the Dream"|"500" order 129823 , wrong sometimes: 500 order 129823 , right 500
The questions isn't is someone likely to put that pipe dream in there, the question is if you should ever have to care if they do!

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (2)
As of 2023-09-26 22:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?