Hi Monks, I have a query. I have an input CSV file that looks like this.
NPLUS,32,0, NW,41,0,1 NWER,51,"0,1,2",12
This is the format Layer name, layer number, data type, text type. some times there are more than 1 data type, in which case they are grouped using "".

For the first line NPLUS is the layer name,32 is layer number and 0 is the data type. There is no text type for this line

For the second line NW is the layer name,41 is layer number and 0 is the data type and 1 is the text type,

For the last line NWER is the layer name, 51 is the layer number, 0,1,2 is the data type and 12 is the text type.

I want to check the fields and print the following.
NPLUS layernumber 32 datatype 0, NW layernumber 41 datatype 0 text 1, NWER layernumber 51 datatype 0 datatype 1 datatype 2 text 12;
I have written a sample code, but I am not able to figure out how to split based on two delimiters and then get the required result. the last line also should have a semicolon, where as other lines should have a coma at the end only. Here is the sample code.
open(my $in, '<', 'Text_File.csv') or die "Cannot open Text_File.csv: $!"; open(my $out, '>', 'mask.spec') or die "Cannot open mask.spec: $!"; while (my $line = <$in>) { #my @fields = split (/(?<="),(?=")/, $line); my @fields = split(/,/, $line); if (@fields == 4) { printf {$out} "%s layernum %s datatype %s,%s", @fields; } elsif (@fields == 5) { printf {$out} "%s layernum %s datatype %s text %s,%s", @fields +; } }
Any help would be greatly appreciated.

In reply to splitting csv file and saving data by Ganesh Bharadwaj1

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.