Extra new line inserted in simple program

P

Paul D

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;
}
 
P

Paul D

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
 
U

Uri Guttman

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

chomp is safer.

uri
 
P

Paul D

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

Thank you both very much for your tips.

Best wishes
Paul
 
C

Chris Mattern

Paul said:
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?"
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top