Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help creating CSV file based on varying value conditions 1

Status
Not open for further replies.

melsterTEK

Programmer
Feb 6, 2007
27
0
0
US
I am trying to create a csv file, the catch is I have to do some data validation and/or pattern matching of some sort to create the final file.

I need to go through the given column where some columns have data values that are separated by commas “,” like so:

Requirements:
-Validate the number of coma separated values
-If validation fails, generate a valid entry with blank coma separated values

Example;
S_YSVACCEPT = "4.12,YSV5,7830" OK, 3 comma separated values
S_YSVACCEPT = "4.12,YSV5," OK, 3 comma separated values

S_YSVACCEPT = "4.12,YSV5" Bad, only 2 comma separated, you should return ", ,"
S_YSVACCEPT = "foo,faa,blah,kaa" Bad, 4 comma separated value, you should return ", ,"
S_YSVACCEPT = "0" Bad, only 1 comma separated value, you should return ", ,"

Can someone help me with this logic? Thanks.
 
instead of:

Code:
$S_YSVACCEPT = '",,"' if ($commas != 2);

use:

Code:
$S_YSVACCEPT = ',,' if ($commas != 2);





- Kevin, perl coder unexceptional! [wiggle]
 
You're the best! Thanks. Now, I need to do this for more than one column now..just apply the same logic right?
Thanks again..I learned a bunch ;->

my $commas2 = $COL2 =~ tr/,/,/;
if ($commas2 !=2) {
$COL2 = '",,"';
} elsif ("$COL2" eq "0") {
$COL2 = '",,"'
};

my $commas3 = $COL3 =~ tr/,/,/;
if ($commas3 !=2) {
$COL3 = '",,"';
} elsif ("$COL3" eq "0") {
$COL3 = '",,"'
};
 
you can maybe reduce the code significantly:

Code:
[black][b]my[/b][/black] [blue]@results[/blue] [black]=[/black] [red]([/red][red])[/red][red];[/red]
[olive][b]while[/b][/olive] [red]([/red][black][b]my[/b][/black] [blue]@data[/blue] [black]=[/black] [blue]$sth[/blue][black]->[/black][maroon]fetchrow_array[/maroon][red]([/red][red])[/red][red])[/red] [red]{[/red]
   [olive][b]for[/b][/olive] [red]([/red][blue]@data[/blue][red])[/red] [red]{[/red]
      [black][b]my[/b][/black] [blue]$commas[/blue] [black]=[/black] [blue]$_[/blue] [black]=~[/black] [red]tr/[/red][purple],[/purple][red]/[/red][purple],[/purple][red]/[/red][red];[/red]
      [blue]$_[/blue] [black]=[/black] [red]'[/red][purple],,[/purple][red]'[/red] [olive][b]if[/b][/olive] [red]([/red][blue]$commas[/blue] [black]!=[/black] [fuchsia]2[/fuchsia][red])[/red][red];[/red]
   [red]}[/red] 
   [black][b]push[/b][/black] [blue]@results[/blue][black],[/black][black][b]join[/b][/black][red]([/red][red]'[/red][purple],[/purple][red]'[/red][black],[/black][blue]@data[/blue][red])[/red][red];[/red]
[red]}[/red]

but are you sure joining comma seperated fields with commas is what you really want to do? Normlly, fields with internal commas would be wrapped in double-quotes in a CSV file.





- Kevin, perl coder unexceptional! [wiggle]
 
You're right. I might just change the delimiter back to double quotes or use a "|" or tab for that matter.

Neat exercise. You taught me a lot today! Thanks again. ;->

-btw how do I used that "code" box? I can't seem to find a link to post code.

 
there is a link just below the form you use to enter replies:

Process TGML

it explains the options for posting formatted code.




- Kevin, perl coder unexceptional! [wiggle]
 
Hi Kevin,
For curiosity, how would you do this using split?
 
this would probably be slower that the other way (untested code):

Code:
my @results = ();
while (my @data = $sth->fetchrow_array()) {
   for (@data) {
      my @fields = split(/,/,$_);
      $_ = ',,' if (scalar @fields != 3);
   }
   push @results,join(',',@data);
}

- Kevin, perl coder unexceptional! [wiggle]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top