Ganesh Bharadwaj1 has asked for the wisdom of the Perl Monks concerning the following question:
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 "".NPLUS,32,0, NW,41,0,1 NWER,51,"0,1,2",12
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.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.NPLUS layernumber 32 datatype 0, NW layernumber 41 datatype 0 text 1, NWER layernumber 51 datatype 0 datatype 1 datatype 2 text 12;
Any help would be greatly appreciated.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 +; } }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: splitting csv file and saving data (updated)
by haukex (Archbishop) on Nov 03, 2016 at 09:56 UTC | |
by BrowserUk (Patriarch) on Nov 03, 2016 at 11:04 UTC | |
by haukex (Archbishop) on Nov 03, 2016 at 11:26 UTC | |
by BrowserUk (Patriarch) on Nov 03, 2016 at 11:45 UTC | |
by haukex (Archbishop) on Nov 03, 2016 at 12:52 UTC | |
by Tux (Canon) on Nov 03, 2016 at 21:11 UTC | |
by BrowserUk (Patriarch) on Nov 03, 2016 at 21:53 UTC | |
by Anonymous Monk on Nov 03, 2016 at 11:27 UTC | |
|
Re: splitting csv file and saving data
by BrowserUk (Patriarch) on Nov 03, 2016 at 10:48 UTC | |
|
Re: splitting csv file and saving data
by kcott (Archbishop) on Nov 03, 2016 at 22:16 UTC |