Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Extra new line inserted in simple program

Reply
Thread Tools

Extra new line inserted in simple program

 
 
Paul D
Guest
Posts: n/a
 
      01-05-2005
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;
}


 
Reply With Quote
 
 
 
 
Paul D
Guest
Posts: n/a
 
      01-05-2005
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


 
Reply With Quote
 
 
 
 
Uri Guttman
Guest
Posts: n/a
 
      01-05-2005
>>>>> "PD" == Paul D <(E-Mail Removed)> 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.velocityreviews.com/forums/(E-Mail Removed) -------- http://www.stemsystems.com
--Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org
 
Reply With Quote
 
Paul D
Guest
Posts: n/a
 
      01-06-2005
Wow - I've never witnessed such a fast response in a newsgroup.

Thank you both very much for your tips.

Best wishes
Paul


 
Reply With Quote
 
Chris Mattern
Guest
Posts: n/a
 
      01-06-2005
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?"
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: 'subprocess.check_output' extra new line? Chris Angelico Python 0 01-04-2013 04:14 PM
'subprocess.check_output' extra new line? Python 0 01-04-2013 03:50 PM
Does return-by-value mean extra copies and extra overhead? mathieu C++ 3 09-04-2009 04:25 PM
simple script inserted in websurvey software works in firefox but not in internet explorer gray_slp Javascript 3 06-27-2007 02:56 AM
Extra Lines inserted for Special Characters (&amp;) lonni4000@swarmail.com XML 2 01-24-2007 08:59 PM



Advertisments