Hi,
I wrote the following perl script to convert the CSV into HTML files. I need to have a check in the csv files i.e. if it contains more than 5 rows it should write the content in another HTML file and it continues till the end of the file. I am getting the ouput in different files but the problem is only the last record is getting added to the file not all records. I am enclosing the code snippet below. It is just a sample, whereas i have 500000 records to split each files of 10000 records. Am I missing a point here. Could anyone correct me where i am wrong? Thanks for your help.
#!c:/perl/bin/perl
use strict;
use warnings;
open(FH,"C:/csv_in/test.csv") || die("cannot open the file $!");
my $header="<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 Tranistion//E
+N\"><HTML><HEAD><TITLE>HTML OUTPUT</TITLE></HEAD><BODY BGCOLOR=\"#eee
+eee\"><TABLE BORDER=1><CAPTION><FONT FACE=\"HELVETICA, ARIAL\"><B><P>
+Test HTML </P></B></FONT></CAPTION><TR><TH><FONT FACE=\"HELVETICA, AR
+IAL\">category</FONT></TH><TH><FONT FACE=\"HELVETICA, ARIAL\"></FONT>
+Name</TH><TH><FONT FACE=\"HELVETICA, ARIAL\">DOB</FONT></TH><TH><FONT
+ FACE=\"HELVETICA, ARIAL\">SEX</FONT></TH></TR>";
my $count=0;
while(my $line=<FH>){
if($line=~/M|F$/){
$count++;
if($count <= 5){
open(OUT,">C:/html_out/output_1.html") or die("cannot open
+ $!");
print OUT $header;
my @fields=split /,/, $line;
print OUT ("<TR>", (map {qq{<TD><FONT FACE=\"HELVETICA, AR
+IAL\"><CENTER>$_</CENTER></FONT></TD>}} @fields), "</TR>");
}elsif($count > 5 && $count <= 10){
open(OUT1,">C:/html_out/output_2.html") or die("cannot ope
+n $!");
print OUT1 $header;
my @fields=split /,/, $line;
print OUT1 ("<TR>", (map {qq{<TD><FONT FACE=\"HELVETICA, A
+RIAL\"><CENTER>$_</CENTER></FONT></TD>}} @fields), "</TR>");
}elsif($count > 10 && $count <= 15){
open(OUT2,">>C:/html_out/output_3.html") or die("cannot op
+en $!");
print OUT2 $header;
my @fields=split /,/, $line;
print OUT2 ("<TR>", (map {qq{<TD><FONT FACE=\"HELVETICA, A
+RIAL\"><CENTER>$_</CENTER></FONT></TD>}} @fields), "</TR>");
}
close OUT;
close OUT1;
close OUT2;
}
#if($count > 5){
}
close FH;
test.csv
--------
Name DOB Sex
John 1/1/1980 M
Kathy 2/2/2987 F
Mike 3/3/1999 M
Stella 4/4/1985 F
Robert 5/5/1991 M
Britto 6/6/1993 M
Katherine 7/7/1991 F
vicky 8/8/1992 M
charles 9/9/1992 M
stephen 10/10/1968 M
Denzel 11/11/1996 M