For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > August 2007 > Printing asterisks in screen









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Printing asterisks in screen
Rodrigo Tavares

2007-08-25, 7:00 pm

Hello,

I bought a book about Programming Perl, there is a
exercise :

1) Enter with a number and print asterisks (1..30)
ex: if you type six have go to screen:

******

See the below code :

if (($num1 >= 1 && $num1 <= 30)
{
for (my $i=1 ; $i < 30 ; $i++)
{
while ($num1 == $i)
{
print "*";
}
}
}

This script is in loop. I don't understand, therefore
the line command while, could be stop after bow.

What's wrong ?

best regards,

Rodrigo Faria



Flickr agora em português. Você clica, todo mundo vê.
http://www.flickr.com.br/
Ken Foskey

2007-08-25, 7:00 pm

On Sat, 2007-08-25 at 16:36 -0300, Rodrigo Tavares wrote:

> if (($num1 >= 1 && $num1 <= 30)
> {
> for (my $i=1 ; $i < 30 ; $i++)
> {
> while ($num1 == $i)
> {
> print "*";
> }
> }
> }



Think in terms of values: Use 6 instead of $num1 and think about what
it says.

if ((6 >= 1 && 6 <= 30) # yes this works
> {
> for (my $i=1 ; $i < 30 ; $i++)
> {
> while (6 == $i) # infinite loop because $i does not change.
> {
> print "*";
> }
> }
> }



--
Ken Foskey
FOSS developer

Gunnar Hjalmarsson

2007-08-25, 7:00 pm

Rodrigo Tavares wrote:
> 1) Enter with a number and print asterisks (1..30)
> ex: if you type six have go to screen:
>
> ******


if ( $num >= 1 and $num <= 30 ) {
print '*' x $num, "\n";
}

> if (($num1 >= 1 && $num1 <= 30)
> {
> for (my $i=1 ; $i < 30 ; $i++)
> {
> while ($num1 == $i)
> {
> print "*";
> }
> }
> }


This is how a loop solution might look like:

if ( $num >= 1 and $num <= 30 ) {
for my $i ( 1..30 ) {
print '*';
last if $num == $i;
}
}

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
John W. Krahn

2007-08-25, 7:00 pm

Rodrigo Tavares wrote:
> Hello,


Hello,

> I bought a book about Programming Perl, there is a
> exercise :
>
> 1) Enter with a number and print asterisks (1..30)
> ex: if you type six have go to screen:
>
> ******
>
> See the below code :
>
> if (($num1 >= 1 && $num1 <= 30)
> {
> for (my $i=1 ; $i < 30 ; $i++)
> {
> while ($num1 == $i)
> {
> print "*";
> }
> }
> }
>
> This script is in loop. I don't understand, therefore
> the line command while, could be stop after bow.
>
> What's wrong ?


You don't need any loops in there:

if ( $num1 >= 1 && $num1 <= 30 )
{
print '*' x $num1;
}



John
--
Perl isn't a toolbox, but a small machine shop where you
can special-order certain sorts of tools at low cost and
in short order. -- Larry Wall
Steven M. O'Neill

2007-08-26, 10:00 pm

Gunnar Hjalmarsson <noreply@gunnar.cc> wrote:
>Rodrigo Tavares wrote:
>
> if ( $num >= 1 and $num <= 30 ) {
> print '*' x $num, "\n";
> }
>
>
>This is how a loop solution might look like:
>
> if ( $num >= 1 and $num <= 30 ) {
> for my $i ( 1..30 ) {
> print '*';
> last if $num == $i;
> }
> }


It's a bit more complicated than need be, no? How about :

print "*" for (1..4);

But for 1,000,000 iterations (printing "" instead of "*"), it's
1.74 CPU seconds for this versus 0.41 CPU seconds for the binary
"x" operator.

--
Steven O'Neill steveo@panix.com
Brooklyn, NY http://www.panix.com/~steveo
Steven M. O'Neill

2007-08-26, 10:00 pm

Gunnar Hjalmarsson <noreply@gunnar.cc> wrote:
>Rodrigo Tavares wrote:
>
> if ( $num >= 1 and $num <= 30 ) {
> print '*' x $num, "\n";
> }
>
>
>This is how a loop solution might look like:
>
> if ( $num >= 1 and $num <= 30 ) {
> for my $i ( 1..30 ) {
> print '*';
> last if $num == $i;
> }
> }


It's a bit more complicated than need be, no? How about :

print "*" for (1..$num);

But for 1,000,000 iterations (printing "" instead of "*", with
$num=4), it's 1.74 CPU seconds for this versus 0.41 CPU seconds
for the binary "x" operator.

--
Steven O'Neill steveo@panix.com
Brooklyn, NY http://www.panix.com/~steveo

--
Steven O'Neill steveo@panix.com
Brooklyn, NY http://www.panix.com/~steveo
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com