A question about EOF

Discussion in 'Perl' started by SL_McManus, Dec 3, 2003.

  1. SL_McManus

    SL_McManus Guest

    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{$line} = 1;
    print OUTFILE "$line \n";
    # if (eof) {
    # chop $line;
    # }
    }
    {
    for ($lineread=0; $lineread<=@firstlist;$lineread++) {
    }
    $x = $lineread / $linecount;
    $x = sprintf ( "%.0f", $x );
    print OUTFILE "CR $x\n";
    }


    close(OUTFILE);
    print "finished!\n";
    1;
    __END__
    SL_McManus, Dec 3, 2003
    #1
    1. Advertising

  2. SL_McManus

    Jim Gibson Guest

    In article <>,
    SL_McManus <> wrote:

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


    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(@firstlist) . "\n";
    }else{
    print OUTFILE $firstlist[$i] . "\n";
    }
    }

    >
    >
    > open(OUTFILE,'>' .$outfile) || die "cannot create $outfile: $!\n";
    >
    > %priminput = ();
    > foreach $line (@firstlist) {
    > $priminput{$line} = 1;
    > print OUTFILE "$line \n";
    > # if (eof) {
    > # chop $line;
    > # }
    > }
    > {
    > for ($lineread=0; $lineread<=@firstlist;$lineread++) {
    > }
    > $x = $lineread / $linecount;
    > $x = sprintf ( "%.0f", $x );
    > print OUTFILE "CR $x\n";
    > }
    >
    >
    > close(OUTFILE);
    > print "finished!\n";
    > 1;
    > __END__


    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.
    Jim Gibson, Dec 4, 2003
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. entropy123

    Newbie Question: EOF in MS Visual Studio 2003?

    entropy123, Jul 2, 2003, in forum: C Programming
    Replies:
    6
    Views:
    1,811
    entropy123
    Jul 3, 2003
  2. shan

    newbie EOF question

    shan, Nov 22, 2003, in forum: C Programming
    Replies:
    36
    Views:
    968
    Dan Pop
    Nov 27, 2003
  3. Kobu
    Replies:
    10
    Views:
    605
    Keith Thompson
    Mar 4, 2005
  4. SpreadTooThin

    ifstream eof not reporting eof?

    SpreadTooThin, Jun 13, 2007, in forum: C++
    Replies:
    10
    Views:
    677
    James Kanze
    Jun 15, 2007
  5. Jan Burse
    Replies:
    67
    Views:
    1,029
    Jan Burse
    Mar 14, 2012
Loading...

Share This Page