Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
Here's a neater version for you:
use strict; use warnings; use English; $OUTPUT_RECORD_SEPARATOR = "\n"; # Open files my $csv_filename = shift; open my $csv_fh, "<", $csv_filename or die "Cannot open $csv_filenam +e file: ", $OS_ERROR; open my $html_fh, '>', 'gen_try.html' or die "Cannot open gen_try.html + file: ", $OS_ERROR; # Print Heading html using heredoc print {$html_fh} <<EOHTML; <HTML> <HEAD> <TITLE>CSV Output</TITLE> </HEAD> <body> <table border=1 cellspacing=0 cellpadding=5> <caption>$csv_filename</caption> EOHTML # Read CSV file while ( my $line = <$csv_fh> ) { chomp $line; # Only for html readability my @fields = split /,/, $line; print {$html_fh} ( '<tr>', ( map {qq{<td>$_</td>}} @fields ), '</t +r>' ) or die "Cannot print to html file: ", $OS_ERROR; } print {$html_fh} <<EOHTML; </table> </HTML> EOHTML close $csv_fh or die "Cannot close $csv_filename file: ", $OS_ERROR; close $html_fh or die "Cannot close output file: ", $OS_ERROR; __END__


Or to make it a bit more compact

use strict; use warnings; use English; $OUTPUT_RECORD_SEPARATOR = "\n"; # Open files my $csv_filename = shift; open my $csv_fh, "<", $csv_filename or die "Cannot open $csv_filenam +e file: ", $OS_ERROR; open my $html_fh, '>', 'gen_try.html' or die "Cannot open gen_try.html + file: ", $OS_ERROR; # Print Heading html using heredoc print {$html_fh} q{<HTML><HEAD><TITLE>CSV Output</TITLE></HEAD><body>< +table border=1 cellspacing=0 cellpadding=5><caption>$csv_filename</ca +ption>}; # Read CSV file while ( my $line = <$csv_fh> ) { chomp $line; # Only for html readability print {$html_fh} ( '<tr>', ( map {qq{<td>$_</td>}} ( split /,/, $l +ine ) ), '</tr>' ) or die "Cannot print to html file: ", $OS_ERROR; } print {$html_fh} q{</table></HTML>}; close $csv_fh or die "Cannot close $csv_filename file: ", $OS_ERROR; close $html_fh or die "Cannot close output file: ", $OS_ERROR; __END__


Ahhhh much better...
Although the map still looks a bit icky
John

In reply to Re: Better Code Solution needed for the below CSV file parsing and printing in HTML format by Anonymous Monk
in thread 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":



  • 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 avoiding work at the Monastery: (7)
As of 2024-04-23 18:39 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found