insta.gator has asked for the wisdom of the Perl Monks concerning the following question:
Hi Monks. I have a comma delimited file. There are always values in fields 0-2 and always in 3 OR 4 but not both 3 AND 4. What I am trying to do is simply read each line and write it out as double-quoted comma delimited data, which I have accomplished. What I don't know how to do is not write out the empty field 3 or 4. Here is my code
open(INFILE1, "$ARGV[0]") or exit(101); open (OUTFILE1, ">" . "delimited_$ARGV[0]") or exit(201); $coid="$ARGV[1]"; if ("$coid" eq ""){$coid="***COID***";} while(<INFILE1>) { @file_line_contents = split (/\,/,$_); print OUTFILE1 "\"$coid\"\,\"".join ('","', @file_line_contents); }
For example, if I have this: AKP,45,MILITARY DIFF,,Exclude , I want to write out this: "***COID***","AKP","45","MILITARY DIFF","Exclude" (notice field 3 was not written out).
Is there a way to do this with a join command? If not, suggestions on how to best do it?
Thanks for any help.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Converting a delimited file.
by Corion (Patriarch) on May 20, 2015 at 19:27 UTC | |
|
Re: Converting a delimited file.
by Laurent_R (Canon) on May 20, 2015 at 20:19 UTC | |
|
Re: Converting a delimited file.
by hdb (Monsignor) on May 20, 2015 at 20:03 UTC | |
|
Re: Converting a delimited file.
by Tux (Canon) on May 21, 2015 at 06:09 UTC | |
|
Re: Converting a delimited file.
by boftx (Deacon) on May 21, 2015 at 09:55 UTC |