Extra new line inserted in simple program

Discussion in 'Perl Misc' started by Paul D, Jan 5, 2005.

  1. Paul D

    Paul D Guest

    Hi all

    Please forgive me for the simplistic and inefficient nature of the following
    program - I am working through the early chapters of a "Teach yourself Perl"
    book.

    Could someone tell me why Program 1 leaves a gap between the first and
    second lines of the descending list of numbers that it outputs, which is in
    contrast to Program 2 which prints a continuous list?

    I'd be very greatful for your explanation

    Many thanks
    Paul



    = = = = = = = Program 1 = = = = = = =
    #!/bin/perl

    print ("Enter a number\n");
    $number = <STDIN>;
    print ("\n\n\n");

    $count = $number;
    $done = 0;
    until ($done == 1) {
    print ($count,"\n");
    if ($count == 1) {
    $done = 1;
    }
    $count = $count - 1;
    }


    = = = = = = = Program 2 = = = = = = =
    #!/bin/perl

    $number = 10;
    print ("\n\n\n");

    $count = $number;
    $done = 0;
    until ($done == 1) {
    print ($count,"\n");
    if ($count == 1) {
    $done = 1;
    }
    $count = $count - 1;
    }
    Paul D, Jan 5, 2005
    #1
    1. Advertising

  2. Paul D

    Paul D Guest

    I am a fool - I have realised that <STDIN> includes an /n.
    Using chop($number); resolves the issue.

    Apologies for wasting a post.

    Thanks
    Paul
    Paul D, Jan 5, 2005
    #2
    1. Advertising

  3. Paul D

    Uri Guttman Guest

    >>>>> "PD" == Paul D <> writes:

    PD> I am a fool - I have realised that <STDIN> includes an /n.
    PD> Using chop($number); resolves the issue.

    chomp is safer.

    uri

    --
    Uri Guttman ------ -------- http://www.stemsystems.com
    --Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
    Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org
    Uri Guttman, Jan 5, 2005
    #3
  4. Paul D

    Paul D Guest

    Wow - I've never witnessed such a fast response in a newsgroup.

    Thank you both very much for your tips.

    Best wishes
    Paul
    Paul D, Jan 6, 2005
    #4
  5. Paul D wrote:

    > Hi all
    >
    > Please forgive me for the simplistic and inefficient nature of the
    > following program - I am working through the early chapters of a "Teach
    > yourself Perl" book.
    >
    > Could someone tell me why Program 1 leaves a gap between the first and
    > second lines of the descending list of numbers that it outputs, which is
    > in contrast to Program 2 which prints a continuous list?
    >
    > I'd be very greatful for your explanation
    >
    > Many thanks
    > Paul
    >
    >
    >

    <snipped>

    Simple. The line you get from STDIN still has the line feed it got
    when you hit enter to enter it. You need to use chomp() to get rid
    of it.

    --
    Christopher Mattern

    "Which one you figure tracked us?"
    "The ugly one, sir."
    "...Could you be more specific?"
    Chris Mattern, Jan 6, 2005
    #5
    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. Replies:
    2
    Views:
    392
  2. mathieu
    Replies:
    3
    Views:
    594
    Bo Persson
    Sep 4, 2009
  3. Guest
    Replies:
    0
    Views:
    99
    Guest
    Jan 4, 2013
  4. Chris Angelico
    Replies:
    0
    Views:
    138
    Chris Angelico
    Jan 4, 2013
  5. Guest
    Replies:
    0
    Views:
    139
    Guest
    Jan 4, 2013
Loading...

Share This Page