while() { @file_line_contents = split (/\,/,$_); # this assumes that $file_line_contents[3] never contains the value '0' $file_line_contents[3] ||= $file_line_contents[4]; # now get rid of the unneeded element (assumes this is the last element in the array) pop @file_line_contents; print OUTFILE1 "\"$coid\"\,\"".join ('","', @file_line_contents); }