Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
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

In reply to Only last record is written to the output file instead of all records by valavanp

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (1)
As of 2024-04-16 16:17 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found