Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

I feel stupid... 2 hours later

by sdyates (Scribe)
on Nov 24, 2012 at 06:41 UTC ( #1005337=perlquestion: print w/ replies, xml ) Need Help??
sdyates has asked for the wisdom of the Perl Monks concerning the following question:

I wrote this code to read an excel csv file with five records per line. However, the <tr> shows up at the beginning of the output file and the </tr> shows up at the end. I want to get them to show up at the beginning and end of each line. Is the probem with my CSV or my code. I can't see straight anymore :(
use CGI qw(:standard); use strict; use warnings; my $line; my $file; my ($f1,$f2,$f3,$f4,$f5); my $output = "new.htm"; open (OUTPUT, ">$output") or die "Cannot open $output"; my $z=0; $file='irregular-verbs.csv'; open(F,$file)||die("Could not open $file"); while ($line=<F>) { print OUTPUT "<tr>"; my @cells= split ',',$line; foreach my $cell (@cells) { print OUTPUT "<td>$cell</td>"; } print OUTPUT "</tr>"; } close(F);
First few lines of the OUTPUT file.
<tr><td>Base Form</td><td>Past Simple</td><td>Past Participle</td><td> +3rd Person Singular</td><td>Present Participle / Gerund Abide</td><td>Abode/Abided</td><td>Abode/Abided/Abidden</td><td>Abides +</td><td>Abiding Aby/Abye</td><td>Abought</td><td>Abought</td><td>Abys/Abyes</td><td>Ab +ying Alight</td><td>Alit/Alighted</td><td>Alit/Alighted</td><td>Alights</td +><td>Alighting Arise</td><td>Arose</td><td>Arisen</td><td>Arises</td><td>Arising Awake</td><td>Awoke</td><td>Awoken</td><td>Awakes</td><td>Awaking
Thanks for your help!

Comment on I feel stupid... 2 hours later
Select or Download Code
Re: I feel stupid... 2 hours later
by 2teez (Priest) on Nov 24, 2012 at 07:10 UTC

    Hi sdyates,
    Why not use the module Text::CSV or Text::CSV_XS to read your CSV files.

    However, the <tr> shows up at the beginning of the output file and the </tr> shows up at the end. I want to get them to show up at the beginning and end of each line

    You could put your code print OUTPUT "<tr>"; and print OUTPUT "</tr>"; within the foreach loop.
    Or you could write like so:

    foreach my $cell (@cells) { print OUTPUT "<tr><td>$cell</td></tr>"; }

    Update: I think I misunderstood, the show up at the beginning and end of each line
    Kenosis's solution shown below is alot better, using Text::CSV.

    If you tell me, I'll forget.
    If you show me, I'll remember.
    if you involve me, I'll understand.
    --- Author unknown to me
Re: I feel stupid... 2 hours later
by monsoon (Pilgrim) on Nov 24, 2012 at 07:15 UTC
    your EOL character in the input file is CR (0x0D), so the entire file reads into 1 line. Change the line separator to CR and chomp your lines.
    $/ = "\r"; while ($line=<F>) { chomp $line; print OUTPUT "<tr>"; my @cells= split ',',$line; foreach my $cell (@cells) { print OUTPUT "<td>$cell</td>"; } print OUTPUT "</tr>\n"; }
      2tees thanks so much for your explanation. I thought about chomp, but thought it was only required when reading STIN from a user. And, yes, it was reading in the entire file. Thanks so much for the explanation. Moonson had the correct solution, but your explanation helped me learn from my mistakes. Its amazing what a few years away can do to a person's skills. I obviously need more practice. Thanks so much!
Re: I feel stupid... 2 hours later
by Kenosis (Priest) on Nov 24, 2012 at 07:37 UTC

    I second 2teez's question about using a CSV module to parse your file. Given this, here's one option:

    use strict; use warnings; use Text::CSV; my $csvFile = 'irregular-verbs.csv'; my $outFile = 'new.htm'; open my $outFH, '>', $outFile or die "Cannot open $outFile: $!"; my $csv = Text::CSV_XS->new( { binary => 1, auto_diag => 2 } ) or die "Cannot use CSV: " . Text::CSV->error_diag(); open my $csvFH, '<', $csvFile or die "Cannot open $csvFile: $!"; # $row contains an array reference to the parsed csv line's data while ( my $row = $csv->getline($csvFH) ) { print $outFH '<tr>'; print $outFH "<td>$_</td>" for @$row; print $outFH "<tr>\n"; } close $csvFH; close $outFH;

    Output to file:

    <tr><td>Base Form</td><td>Past Simple</td><td>Past Participle</td><td> +3rd Person Singular</td><td>Present Participle / Gerund</td><tr> <tr><td>Abide</td><td>Abode/Abided</td><td>Abode/Abided/Abidden</td><t +d>Abides</td><td>Abiding</td><tr> <tr><td>Aby/Abye</td><td>Abought</td><td>Abought</td><td>Abys/Abyes</t +d><td>Abying</td><tr> <tr><td>Alight</td><td>Alit/Alighted</td><td>Alit/Alighted</td><td>Ali +ghts</td><td>Alighting</td><tr> <tr><td>Arise</td><td>Arose</td><td>Arisen</td><td>Arises</td><td>Aris +ing</td><tr> <tr><td>Awake</td><td>Awoke</td><td>Awoken</td><td>Awakes</td><td>Awak +ing</td><tr>

    Hope this helps!

Re: I feel stupid... 2 hours later
by Anonymous Monk on Nov 24, 2012 at 10:56 UTC

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1005337]
Approved by Kenosis
Front-paged by 2teez
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (3)
As of 2015-07-05 18:13 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (67 votes), past polls