Delimited output in perl

In the previous code, i got the desired output due to pipe limited. in the new format i have recieved as comma delimited, with in the field commas are already present with " " . so i am unable to get the desired output.

21W34|8720598361193|10001||"Amandelen, ongebrand"|320

previous code

 my @elem = split /\|/;

expected output

21W34
8720598361193
10001
Amandelen, ongebrand
320

current data ( After 320 data, we used to get more like single field -> "data1, data2" or
single field -> data1 single field ->data2.

21W34,8720598361193,10001,,"Amandelen, ongebrand",320

current code

my @elem = split /\,/;
current output 

21W34
8720598361193
10001
"Amandelen
ongebrand"
320

expected output 

21W34
8720598361193
10001
Amandelen, ongebrand
320


Global examlple . 

field1,field2,field3
foo, "bar, baz", baz

CSV seems a simple code that can be handled with split commands. And it can be done that way as long as the CSV file is simple. But when things get more complicated you need a CSV parser. It will take care of strings enclosed in double quotes. And double quotes in strings (should be encoded as double double-quotes). And newlines in strings.
You could write such a parser yourself, but luckily someone already did so: Text::CSV. Read all about it in How to read a CSV file using Perl.

2 Likes

Yes It works using | pipe delimited. I would like to try in the comma delimited.
If the case is below i am not getting the desired output required using the split command.

field1,field2,field3:
foo, "bar, baz", baz

output expected :
foo
"bar,baz"
baz

Continue here: