Hi Monks,
I wrote the following scripts for parsing the CSV and printing in an HTML format.
Method 1:
#!c:/perl/bin/perl use strict; use warnings; #opening the csv file my $caption=pop(@ARGV); open(FH,$caption) || die("Cannot open $caption file"); my($line,$id,$name,$age,@records,@out); @out="<HTML><HEAD><TITLE>CSV Output</TITLE></HEAD><body><table border= +1><tr><td>ID</td><td>NAME</td><td>AGE</td>"; #creating a new file for the html output and writing the header conten +t open(FH1,">output.html"); print FH1 @out; #splitting the comma separated values and writing it to the html file while($line=<FH>){ next if($line=~/^ID/); chomp($line); ($id,$name,$age)=split(/,/,$line); print FH1 "<tr><td>$id</td><td>$name</td><td>$age</td></tr>"; } print FH1 "</table>"; close FH1; close FH;
The CSV file is:
ID,NAME,AGE 1,JOHN,21 2,MARK,32 3,CATHY,21
The HTML output which i got is:
<HTML><HEAD><TITLE>CSV Output</TITLE></HEAD><body><table border=1><tr> +<td>ID</td><td>NAME</td><td>AGE</td><tr><td>1</td><td>Alex</td><td>24 +</td></tr><tr><td>2</td><td>Bret</td><td>21</td></tr><tr><td>3</td><t +d>Cathy</td><td>24</td></tr></table>
Method 2:
#!c:/perl/bin/perl use strict; use warnings; $\="\n"; my($caption,$line,@out,@rec,$x,@output); #opening the csv file $caption=pop(@ARGV); open(FH,$caption) || die("Cannot open $caption file"); #print '<table border cellspacing=0 cellpadding=5>'; #print '<caption>' . $caption . '</caption>'; @out="<HTML><HEAD><TITLE>CSV Output</TITLE></HEAD><body><table border +cellspacing=0 cellpadding=5><caption>$caption</caption>"; open(FH1,">gen_try.html"); print FH1 @out; while($line=<FH>){ chomp($line) ; @rec = split(/,/, $line); $x=0; print '<tr>'; while($x <= $#rec){ print FH1 "\t <td>". $rec[$x] . '</td>'; ++$x; } print '</tr>'; } print '</table>';

When you compare both the methods, in the second method i didn't hard code the column headings directly in my script. But the html output which i am getting is not like as method1. Could you please any one suggest me a solution in which it should be generic not like method1. Please suggest me without using any modules like Text::CSV. I need the solution with out using modules.

In reply to Better Code Solution needed for the below CSV file parsing and printing in HTML format by valavanp

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



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.