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 bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Append Commas 1

Status
Not open for further replies.

mte0910

Programmer
Apr 20, 2005
232
US
I have a csv file with some lines having more "fields", which are represented between the commas, than others. I need all records to contain the same number of commas. I used the code below to figure out how many commas are in each line, now I need to add however many commas should come at the end. Just not sure how to start.

open (IN, 'C:\Input.csv');
open (OUT,'>C:\Output.csv');
while ($Line4 = <IN>) {
chomp;
$count4 = ($Line4 =~tr/,//);
print OUT "$count4-$Line4";
}
close (IN); close (OUT);


Input(is):
data1,data2,data3,data4
data1,data2,
data1,,data3,data4
data1,data2,data3

Output(needs to be):
data1,data2,data3,data4
data1,data2,,
data1,,data3,data4
data1,data2,data3,
 
I have this which I use to count curly brackets in a test file
Code:
$curl= () = $part =~ /{/g;
I can't even remember how it works but it does, it assigns the number ooccurrences of '{' in the var $part, to $curl.

hope this helps

Keith
 
I got it to work with the code below, but I bet it will make you laugh. I'm sure it could be done much smoother, but I am an admitted newbie....


open (IN, 'D:\CBC\MCP\CBC1.csv');
open (OUT,'>D:\CBC\MCP\CBC2.csv');
while ($Line4 = <IN>) {
chomp ($Line4);
$count4 = ($Line4 =~tr/,//);
if($count4 == 1){
print OUT "$Line4,,,,,,,,,,,,,\n";
}
if($count4 == 2){
print OUT "$Line4,,,,,,,,,,,,\n";
}
if($count4 == 3){
print OUT "$Line4,,,,,,,,,,,\n";
}
if($count4 == 4){
print OUT "$Line4,,,,,,,,,,\n";
}
if($count4 == 5){
print OUT "$Line4,,,,,,,,,\n";
}
if($count4 == 6){
print OUT "$Line4,,,,,,,,\n";
}
if($count4 == 7){
print OUT "$Line4,,,,,,,\n";
}
if($count4 == 8){
print OUT "$Line4,,,,,,\n";
}
if($count4 == 9){
print OUT "$Line4,,,,,\n";
}
if($count4 == 10){
print OUT "$Line4,,,,\n";
}
if($count4 == 11){
print OUT "$Line4,,,\n";
}
if($count4 == 12){
print OUT "$Line4,,\n";
}
if($count4 == 13){
print OUT "$Line4,\n";
}
if($count4 >= 14){
print OUT "$Line4\n";
}
}
close (IN); close (OUT);
 
All you have to do is subtract the counted number of commas from the required number of commas then add them to the end. Assuming 14 is the required number:

Code:
[url=http://perldoc.perl.org/functions/use.html][black][b]use[/b][/black][/url] [green]strict[/green][red];[/red]
[black][b]use[/b][/black] [green]warnings[/green][red];[/red]
[url=http://perldoc.perl.org/functions/my.html][black][b]my[/b][/black][/url] [blue]$file[/blue] = [red]'[/red][purple]D:\CBC\MCP\CBC1.csv[/purple][red]'[/red][red];[/red]
[red]{[/red]
   [url=http://perldoc.perl.org/functions/local.html][black][b]local[/b][/black][/url] [blue]@ARGV[/blue] = [red]([/red][blue]$file[/blue][red])[/red][red];[/red]
   [black][b]local[/b][/black] [blue]$^I[/blue] = [red]'[/red][purple].bac[/purple][red]'[/red][red];[/red][gray][i]# inplace edit mode[/i][/gray]
   [olive][b]while[/b][/olive] [red]([/red]<>[red])[/red] [red]{[/red]
      [url=http://perldoc.perl.org/functions/chomp.html][black][b]chomp[/b][/black][/url][red];[/red]
      [black][b]my[/b][/black] [blue]$v[/blue] = [blue]$_[/blue][red];[/red]
      [black][b]my[/b][/black] [blue]$c[/blue] = [red]([/red][blue]$v[/blue] =~ [red]tr/[/red][purple],[/purple][red]/[/red][purple],[/purple][red]/[/red][red])[/red][red];[/red]
      [url=http://perldoc.perl.org/functions/print.html][black][b]print[/b][/black][/url] [blue]$_[/blue], [red]'[/red][purple],[/purple][red]'[/red] x [red]([/red][fuchsia]14[/fuchsia]-[blue]$c[/blue][red])[/red], [red]"[/red][purple][purple][b]\n[/b][/purple][/purple][red]"[/red][red];[/red] 
   [red]}[/red]
[red]}[/red]
[black][b]print[/b][/black] [red]"[/red][purple]finished[/purple][red]"[/red][red];[/red]
[tt]------------------------------------------------------------
Pragmas (perl 5.10.0) used :
[ul]
[li]strict - Perl pragma to restrict unsafe constructs[/li]
[li]warnings - Perl pragma to control optional warnings[/li]
[/ul]
[/tt]


As always, test before using on your sensitive files.


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

Part and Inventory Search

Sponsor

Back
Top