For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > August 2007 > A simple program









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 A simple program
Rodrigo Tavares

2007-07-27, 6:59 pm

Hello,

How I can to make this program, where the variable is
enter though <STDIN>, how separete the digits ?


Exercice
01)Write a program than user type five digits,
separates the individual digits of number, and print
the five digits time five, the four digit four times,
thus for ahead.

Best regards,

Rodrigo Faria





Alertas do Yahoo! Mail em seu celular. Saiba mais em http://br.mobile.yahoo.com/mailalertas/
Rodrick Brown

2007-07-28, 3:59 am

On 7/27/07, Rodrigo Tavares <digolinopage@yahoo.com.br> wrote:
> Hello,
>
> How I can to make this program, where the variable is
> enter though <STDIN>, how separete the digits ?
>

use split

>
> Exercice
> 01)Write a program than user type five digits,
> separates the individual digits of number, and print
> the five digits time five, the four digit four times,
> thus for ahead.
>


#!/usr/bin/perl -w
use strict;
use warnings;

print "Enter a digit: ";
my $digit = int(<STDIN> );
print map { next unless $_ =~ /^\d+$/; $_ * $_,"\n" } split//,$digit;

> Best regards,
>
> Rodrigo Faria
>
>
>
>
>
> Alertas do Yahoo! Mail em seu celular. Saiba mais em http://br.mobile.yahoo.com/mailalertas/
>
> --
> To unsubscribe, e-mail: beginners-unsubscribe@perl.org
> For additional commands, e-mail: beginners-help@perl.org
> http://learn.perl.org/
>
>
>



--
Rodrick R. Brown
http://www.rodrickbrown.com
John W. Krahn

2007-07-28, 3:59 am

Rodrick Brown wrote:
> On 7/27/07, Rodrigo Tavares <digolinopage@yahoo.com.br> wrote:
> use split
>
>
> #!/usr/bin/perl -w
> use strict;
> use warnings;
>
> print "Enter a digit: ";
> my $digit = int(<STDIN> );
> print map { next unless $_ =~ /^\d+$/; $_ * $_,"\n" } split//,$digit;


perldoc -f next
next LABEL
next The "next" command is like the "continue" statement in C; it
starts the next iteration of the loop:

LINE: while (<STDIN> ) {
next LINE if /^#/; # discard comments
#...
}

Note that if there were a "continue" block on the above, it would
get executed even on discarded lines. If the LABEL is omitted,
the command refers to the innermost enclosing loop.

"next" cannot be used to exit a block which returns a value such
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
^^^^^^^^^^^^^^^^^^^^^^^^
as "eval {}", "sub {}" or "do {}", and should not be used to exit
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
^^^^^^^^^^^^^^^^^^^^^^^^^
a grep() or map() operation.
^^^^^^^^^^^^^^^^^^^^^^^^^^^^



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
Rodrigo Tavares

2007-07-28, 6:59 pm

Hello,

In my first lesson, not extist split.
And without split ?
There is a mode more simple ?

Best regards,

Rodrigo Faria




--- Rodrick Brown <rodrick.brown@gmail.com> escreveu:

> On 7/27/07, Rodrigo Tavares
> <digolinopage@yahoo.com.br> wrote:
> is
> use split
>
> print
> times,
>
> #!/usr/bin/perl -w
> use strict;
> use warnings;
>
> print "Enter a digit: ";
> my $digit = int(<STDIN> );
> print map { next unless $_ =~ /^\d+$/; $_ * $_,"\n"
> } split//,$digit;
>
> mais em http://br.mobile.yahoo.com/mailalertas/
> beginners-unsubscribe@perl.org
> beginners-help@perl.org
>
>
> --
> Rodrick R. Brown
> http://www.rodrickbrown.com
>




Flickr agora em português. Você cria, todo mundo vê.
http://www.flickr.com.br/
Rodrigo Tavares

2007-07-31, 6:59 pm

Hello,

I got make a script, see below :

#!/usr/bin/perl

use strict;
use warnings;

print "Enter 5 digits: ";
my $digit = <STDIN>;

my $cont = length($digit) - 1;
if ($cont > 5)
{
print "Only five digits ! \n"
}
else
{
my @arrow = split(//, $digit);
print "$arrow[0]\n";
print "$arrow[1],$arrow[1]\n";
print "$arrow[2],$arrow[2],$arrow[2]\n";
print " $arrow[3],$arrow[3],$arrow[3],$arrow[3]\
n";
print
" $arrow[4],$arrow[4],$arrow[4],$arrow[4],
$arrow[4]\n";
}


Best regards,

Rodrigo Faria

--- "John W. Krahn" <krahnj@telus.net> escreveu:

> Rodrick Brown wrote:
> <digolinopage@yahoo.com.br> wrote:
> variable is
> print
> times,
> $_,"\n" } split//,$digit;
>
> perldoc -f next
> next LABEL
> next The "next" command is like the
> "continue" statement in C; it
> starts the next iteration of the loop:
>
> LINE: while (<STDIN> ) {
> next LINE if /^#/; #
> discard comments
> #...
> }
>
> Note that if there were a "continue"
> block on the above, it would
> get executed even on discarded lines.
> If the LABEL is omitted,
> the command refers to the innermost
> enclosing loop.
>
> "next" cannot be used to exit a block
> which returns a value such
>
>

^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
^^^^^^^^^^^^^^^^^^^^^^^^
> as "eval {}", "sub {}" or "do {}", and
> should not be used to exit
>
>

^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
^^^^^^^^^^^^^^^^^^^^^^^^^

> a grep() or map() operation.
> ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
>
>
>
> 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
>
> --
> To unsubscribe, e-mail:
> beginners-unsubscribe@perl.org
> For additional commands, e-mail:
> beginners-help@perl.org
> http://learn.perl.org/
>
>
>




Alertas do Yahoo! Mail em seu celular. Saiba mais em http://br.mobile.yahoo.com/mailalertas/
John W. Krahn

2007-07-31, 6:59 pm

Rodrigo Tavares wrote:
> Hello,


Hello,

> I got make a script, see below :
>
> #!/usr/bin/perl
>
> use strict;
> use warnings;
>
> print "Enter 5 digits: ";
> my $digit = <STDIN>;
>
> my $cont = length($digit) - 1;
> if ($cont > 5)
> {
> print "Only five digits ! \n"
> }
> else
> {
> my @arrow = split(//, $digit);
> print "$arrow[0]\n";
> print "$arrow[1],$arrow[1]\n";
> print "$arrow[2],$arrow[2],$arrow[2]\n";
> print " $arrow[3],$arrow[3],$arrow[3],$arrow[3]\
n";
> print
> " $arrow[4],$arrow[4],$arrow[4],$arrow[4],
$arrow[4]\n";
> }


Or more simply as:

#!/usr/bin/perl

use strict;
use warnings;

print 'Enter 5 digits: ';
( my @arrow = <STDIN> =~ /\d/g ) == 5 or die "Only five digits !\n";

for ( 0 .. $#arrow ) {
print join( ',', ( $arrow[ $_ ] ) x ( $_ + 1 ) ), "\n";
}

__END__



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
Chas Owens

2007-07-31, 6:59 pm

On 7/31/07, John W. Krahn <krahnj@telus.net> wrote:
snip
> Or more simply as:
>
> #!/usr/bin/perl
>
> use strict;
> use warnings;
>
> print 'Enter 5 digits: ';
> ( my @arrow = <STDIN> =~ /\d/g ) == 5 or die "Only five digits !\n";
>
> for ( 0 .. $#arrow ) {
> print join( ',', ( $arrow[ $_ ] ) x ( $_ + 1 ) ), "\n";
> }
>
> __END__

snip

Two issues:
this doesn't fail on "foo1bar2baz345"
it does fail on "1234" (the original doesn't)

I was thinking

#!/usr/bin/perl

use strict;
use warnings;

print 'Enter 5 digits: ';
my @a = <STDIN> =~ /^(\d)(\d)?(\d)?(\d)?(\d)?$/g
or die "Only five digits !\n";
my $i = 1;
print map { $_ x $i++, "\n" } @a;
Chas Owens

2007-07-31, 6:59 pm

On 7/31/07, Chas Owens <chas.owens@gmail.com> wrote:
> On 7/31/07, John W. Krahn <krahnj@telus.net> wrote:
> snip
> snip
>
> Two issues:
> this doesn't fail on "foo1bar2baz345"
> it does fail on "1234" (the original doesn't)
>
> I was thinking
>
> #!/usr/bin/perl
>
> use strict;
> use warnings;
>
> print 'Enter 5 digits: ';
> my @a = <STDIN> =~ /^(\d)(\d)?(\d)?(\d)?(\d)?$/g
> or die "Only five digits !\n";
> my $i = 1;
> print map { $_ x $i++, "\n" } @a;
>


oops, the map should be

print map { join(",", ($_) x $i++),"\n" } @a;
John W. Krahn

2007-07-31, 6:59 pm

Chas Owens wrote:
> On 7/31/07, John W. Krahn <krahnj@telus.net> wrote:
> snip
> snip
>
> Two issues:
> this doesn't fail on "foo1bar2baz345"
> it does fail on "1234" (the original doesn't)
>
> I was thinking
>
> #!/usr/bin/perl
>
> use strict;
> use warnings;
>
> print 'Enter 5 digits: ';
> my @a = <STDIN> =~ /^(\d)(\d)?(\d)?(\d)?(\d)?$/g


You are using the /g option but the pattern is anchored at the beginning and
ending of the line so it will only match once making the /g option
superfluous. (Even if you added the /m option it would still only contain one
line because the readline is in scalar context so the /g option would still be
superfluous. :-)


> or die "Only five digits !\n";
> my $i = 1;
> print map { $_ x $i++, "\n" } @a;



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
Rodrigo Tavares

2007-07-31, 6:59 pm

Hello John,

I don't understood the line in loop for the # in
variable arrow.
How It can to be used ?

Best regards,

Rodrigo


--- "John W. Krahn" <krahnj@telus.net> escreveu:

> Rodrigo Tavares wrote:
>
> Hello,
>
> " $arrow[3],$arrow[3],$arrow[3],$arrow[3]\
n";
>

" $arrow[4],$arrow[4],$arrow[4],$arrow[4],
$arrow[4]\n";
>
> Or more simply as:
>
> #!/usr/bin/perl
>
> use strict;
> use warnings;
>
> print 'Enter 5 digits: ';
> ( my @arrow = <STDIN> =~ /\d/g ) == 5 or die "Only
> five digits !\n";
>
> for ( 0 .. $#arrow ) {
> print join( ',', ( $arrow[ $_ ] ) x ( $_ + 1 )
> ), "\n";
> }
>
> __END__
>
>
>
> 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
>
> --
> To unsubscribe, e-mail:
> beginners-unsubscribe@perl.org
> For additional commands, e-mail:
> beginners-help@perl.org
> http://learn.perl.org/
>
>
>




Alertas do Yahoo! Mail em seu celular. Saiba mais em http://br.mobile.yahoo.com/mailalertas/
Chas Owens

2007-07-31, 6:59 pm

On 7/31/07, John W. Krahn <krahnj@telus.net> wrote:
snip
>
> You are using the /g option but the pattern is anchored at the beginning and
> ending of the line so it will only match once making the /g option
> superfluous. (Even if you added the /m option it would still only contain one
> line because the readline is in scalar context so the /g option would still be
> superfluous. :-)

snip

Yep, it was left over from trying to make a simpler regex to do what I
wanted. Luckily it was superfluous rather than harmful.
Rodrigo Tavares

2007-07-31, 6:59 pm

Hello John,

You could explain the below line ?

print join( ',', ( $arrow[ $_ ] ) x ( $_ + 1 ) ),
"\n";

Thanks,

Rodrigo Faria


--- "John W. Krahn" <krahnj@telus.net> escreveu:

> Chas Owens wrote:
> wrote:
> "Only five digits !\n";
> ) ), "\n";
>
> You are using the /g option but the pattern is
> anchored at the beginning and
> ending of the line so it will only match once making
> the /g option
> superfluous. (Even if you added the /m option it
> would still only contain one
> line because the readline is in scalar context so
> the /g option would still be
> superfluous. :-)
>
>
>
>
> 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
>
> --
> To unsubscribe, e-mail:
> beginners-unsubscribe@perl.org
> For additional commands, e-mail:
> beginners-help@perl.org
> http://learn.perl.org/
>
>
>




Alertas do Yahoo! Mail em seu celular. Saiba mais em http://br.mobile.yahoo.com/mailalertas/
Chas Owens

2007-07-31, 10:00 pm

On 7/31/07, Rodrigo Tavares <digolinopage@yahoo.com.br> wrote:
> Hello John,
>
> You could explain the below line ?
>
> print join( ',', ( $arrow[ $_ ] ) x ( $_ + 1 ) ),
> "\n";

snip

x is the repetition operator.

When the left side is a scalar it builds a string that contains the
left side repeated the right side times: "a" x 5 == "aaaaa". When
the left side is a list it creates a list with each element repeated
the right side times (in the original order): (1, 2, 3) x 3 == (1, 2,
3, 1, 2, 3, 1, 2, 3). So in the example above $_ is the offset into
the array. For the first element it is 0 so we have

print join(",", ($arrow[0]) x 1), "\n";

which is the same as

print join(",", $arrow[0]), "\n";

for the second element $_ is 1 so we have

print join(",", ($arrow[1]) x 2), "\n";

which is the same as

print join(",", $arrow[1], $arrow[1]), "\n";

for the third element $_ is 2 so we have

print join(",", ($arrow[2]) x 3), "\n";

which is the same as

print join(",", $arrow[2], $arrow[2], $arrow[2]), "\n";

and so on.
Chas Owens

2007-07-31, 10:00 pm

On 7/31/07, Chas Owens <chas.owens@gmail.com> wrote:
snip
> print join(",", ($arrow[2]) x 3), "\n";
>
> which is the same as
>
> print join(",", $arrow[2], $arrow[2], $arrow[2]), "\n";

snip

It is important to note that

print join(",", (time()) x 3), "\n";

is not the same as

print join(",", time(), time(), time()), "\n";

time() is only evaluated once and then repeated.
Jeff Pang

2007-08-01, 4:00 am


--- Chas Owens <chas.owens@gmail.com> wrote:
>
> oops, the map should be
>
> print map { join(",", ($_) x $i++),"\n" } @a;
>


Also using for loop is may better since many ppl on
this list said map would consume too much memory if
the input array is large enough.

print join(",", ($_) x ++$i),"\n" for @a;

--
http://home.arcor.de/jeffpang/
http://home.earthlink.net/~pangj/



________________________________________
________________________________________
____
Pinpoint customers who are looking for what you sell.
http://searchmarketing.yahoo.com/
Chas Owens

2007-08-01, 4:00 am

On 7/31/07, Jeff Pang <jeff.pang@yahoo.com> wrote:
>
> --- Chas Owens <chas.owens@gmail.com> wrote:
>
> Also using for loop is may better since many ppl on
> this list said map would consume too much memory if
> the input array is large enough.
>
> print join(",", ($_) x ++$i),"\n" for @a;


Whereas it is true that map uses need more memory than a for loop in
Perl 5*, this is generally not a problem with arrays that are not tied
objects since arrays should be fairly small (under 10,000 items). If
your arrays are very large you probably should not be using arrays in
the first place. In this specific case the number of items in the
array is guaranteed to be between 0 and 5.

* Perl 6 is getting at least some lazy evaluation, so it may not have
this problem.
Dr.Ruud

2007-08-06, 6:59 pm

Rodrigo Tavares schreef:

> Write a program than user type five digits,
> separates the individual digits of number, and print
> the five digits time five, the four digit four times,
> thus for ahead.


#!/usr/bin/perl
use strict;
use warnings;

my $n = 5; # number-of-digits
print "Enter $n digits: ";

my $input = <>; # read from STDIN
$input =~ s/\D+//g; # remove non-digits

length( $input ) == $n
or die ":-( I said $n digits!\n";

for my $i ( reverse 1 .. $n ) {
print substr( $input, $i - 1, 1 ) x $i, "\n";
}

--
Affijn, Ruud

"Gewoon is een tijger."
Sponsored Links







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

Copyright 2009 codecomments.com