You talk about a "csv file" and use "/,/" as your split expression, but the data you posted is whitespace-delimited. Apart from that, shouldn't you be using a CSV module, or do you really have perfect confidence that your input data will never have things like quoted fields with embedded delimiters?

Your "if ... elsif ... elsif ..." structure is really not sustainable if you ever need to adapt to input files of arbitrary length. Personally, given a stable (but potentially changeable) csv file (or other database-like source), I would be inclined to use CGI and have a process that delivers a user-specified quantity of data rows starting at a user-specified point, with a button to move back and forth by pages -- similar to what is done here at the Monastery (Nodes You Wrote Perl Monks User Search being a very good example).

But if you just want to generate a set of static html files from your data, I'd do that like this:

#!/usr/bin/perl use strict; use warnings; my $input = ( @ARGV and -f $ARGV[0] ) ? shift : "C:/csv_in/test.csv"; open( $ifh, "<", $input ) or die "$input: open failed: $!\n"; my $out_num = my $out_count = 0; my $ofh; while ( <$ifh> ) { if ( ! defined( $ofh ) or $out_count == 5 ) { if ( $ofh ) { # print closing html stuff (page trailer, etc)... close $ofh; } my $out_name = sprintf( "C:/html_out/output_%d.html", ++$out_n +um ); open( $ofh, ">", $out_name ) or die "$out_name: cannot open: $ +!\n"; # print opening html stuff... $out_count = 0; } # extract fields from data row and stuff it into html... print $ofh ... $out_count++; } # print closing html content to current $ofh ...
That should avoid the problem you were having with so much of the data being absent from the output files.

(Update: I had forgotten that "Nodes You Wrote" was a personal tweak to one's personal "nodelet" set -- which is easy to set up, just follow the original link above -- whereas "Perl Monks User Search" is the direct link to the facility.)


In reply to Re: Only last record is written to the output file instead of all records by graff
in thread 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":



  • 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.