A question about EOF

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • SL_McManus

    A question about EOF

    Hi All;

    I'm fairly new to PERL. The problem I am running into is that I
    need to chop the last line of the output file and place the count
    record on that line. As it stands, there is a blank space and this
    creates a problem. What can I do to test for an EOF condition, then
    chop the last line? Thanks.


    open(OUTFILE,'> ' .$outfile) || die "cannot create $outfile: $!\n";

    %priminput = ();
    foreach $line (@firstlist) {
    $priminput{$lin e} = 1;
    print OUTFILE "$line \n";
    # if (eof) {
    # chop $line;
    # }
    }
    {
    for ($lineread=0; $lineread<=@fir stlist;$linerea d++) {
    }
    $x = $lineread / $linecount;
    $x = sprintf ( "%.0f", $x );
    print OUTFILE "CR $x\n";
    }


    close(OUTFILE);
    print "finished!\ n";
    1;
    __END__
  • Jim Gibson

    #2
    Re: A question about EOF

    In article <b2ecd45e.03120 31145.59459119@ posting.google. com>,
    SL_McManus <slmcmanus67@ne tzero.net> wrote:
    [color=blue]
    > Hi All;
    >
    > I'm fairly new to PERL. The problem I am running into is that I
    > need to chop the last line of the output file and place the count
    > record on that line. As it stands, there is a blank space and this
    > creates a problem. What can I do to test for an EOF condition, then
    > chop the last line? Thanks.[/color]

    What EOF are you looking for? The output file? Or some input file, that
    you haven't shown? The program below does not run because $outfile has
    not been defined and $linecount is zero.

    If you are wanting to know what is the last element of @array, use the
    $#array variable, which is the subscript of the last element. You can
    do a loop like the following:

    for my $i ( 0 .. $#firstlist ) {
    if( $i == $#firstlist ) {
    print OUTFILE $firstlist[$i] . scalar(@firstli st) . "\n";
    }else{
    print OUTFILE $firstlist[$i] . "\n";
    }
    }
    [color=blue]
    >
    >
    > open(OUTFILE,'> ' .$outfile) || die "cannot create $outfile: $!\n";
    >
    > %priminput = ();
    > foreach $line (@firstlist) {
    > $priminput{$lin e} = 1;
    > print OUTFILE "$line \n";
    > # if (eof) {
    > # chop $line;
    > # }
    > }
    > {
    > for ($lineread=0; $lineread<=@fir stlist;$linerea d++) {
    > }
    > $x = $lineread / $linecount;
    > $x = sprintf ( "%.0f", $x );
    > print OUTFILE "CR $x\n";
    > }
    >
    >
    > close(OUTFILE);
    > print "finished!\ n";
    > 1;
    > __END__[/color]

    You will get better answers if you post complete, working programs that
    are as short as possible.

    Also, this group is defunct and shouldn't be used. Try
    comp.lang.perl. misc in the future for better responses.

    Comment

    Working...