<STDIN>

<STDIN>

am 05.09.2011 21:10:35 von Chris Stinemetz

--20cf3079bbfa3b51fe04ac367784
Content-Type: text/plain; charset=ISO-8859-1

In exercise 3-3 from the lama book.

How do you make sure the user enters a number 1 thru 7?

The simple program is:

#!/usr/bin/perl
use warnings;
use strict;
my @names = qw/ fred betty barney dino wilma pebbles bamm-bamm /;
print "Enter some numbers from 1 to 7, one per line, then press Ctrl-Z:\n";
chomp(my @numbers = );
foreach (@numbers) {
print "$names[ $_ -1]\n";
}

Just curious.. Thanks.

--20cf3079bbfa3b51fe04ac367784--

Re: <STDIN>

am 05.09.2011 21:22:16 von Shlomi Fish

On Mon, 5 Sep 2011 14:10:35 -0500
Chris Stinemetz wrote:

> In exercise 3-3 from the lama book.
>=20
> How do you make sure the user enters a number 1 thru 7?
>=20
> The simple program is:
>=20
> #!/usr/bin/perl
> use warnings;
> use strict;
> my @names =3D qw/ fred betty barney dino wilma pebbles bamm-bamm /;
> print "Enter some numbers from 1 to 7, one per line, then press Ctrl-Z:\n=
";
> chomp(my @numbers =3D );
> foreach (@numbers) {
> print "$names[ $_ -1]\n";
> }
>=20

One option would be to check that $_ matches the regular
expression /\A[1-7]\z/ , where \A is the start of the string, [1-7] is any
character from 1 to 7 and \z is the end of the string. So your program beco=
mes:

#!/usr/bin/perl

use warnings;
use strict;

my @names =3D qw/ fred betty barney dino wilma pebbles bamm-bamm /;
print "Enter some numbers from 1 to 7, one per line, then press Ctrl-Z:\n";
chomp(my @numbers =3D );
foreach (@numbers) {
if ($_ =3D~ /\A[1-7]\z/) {
print "$names[ $_ -1]\n";
}
else {
warn "Incorrect number - '$_'";
}
}

I should also note that one would use Ctrl+D instead of Ctrl+Z for EOF in
UNIX-land.

Regards,

Shlomi Fish

> Just curious.. Thanks.



--=20
------------------------------------------------------------ -----
Shlomi Fish http://www.shlomifish.org/
Funny Anti-Terrorism Story - http://shlom.in/enemy

Chuck Norris is his own boss. If you hire him, heâ€=99ll tell your boss=
what to
do.

Please reply to list if it's a mailing list post - http://shlom.in/reply .

--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Re: <STDIN>

am 05.09.2011 21:41:52 von Chris Stinemetz

>
> I should also note that one would use Ctrl+D instead of Ctrl+Z for EOF in
> UNIX-land.
>

Thank you Shlomi. I am currently in the windows-land.
I was not aware of the warn function. Learn something new everyday!

--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Re: <STDIN>

am 05.09.2011 22:14:59 von Shawn H Corey

On 11-09-05 03:22 PM, Shlomi Fish wrote:
> One option would be to check that $_ matches the regular
> expression /\A[1-7]\z/

Another is to write a sub you can re-use:

#!/usr/bin/env perl

use strict;
use warnings;

use Scalar::Util qw( looks_like_number );

sub choose_from_menu {
my ( @menu ) = @_;
my %choice = (); # get unique list of choices

# do loop forever, i.e. until exited by last
GET_ITEM_CHOICES:
while(1){

# prompt for input
print "\nEnter the number(s) of your choice:\n";
print "Press Ctrl-Z to signify you have finished inputting your
list.\n\n";

# display the menu, with a number for each item
my $index = 1;
for my $item ( @menu ){
printf "\t% 2d. %s\n", $index, $item;
}continue{
$index ++;
}

# can get only one choice at a time
print "\n\tEnter your choice: ";
my $anything = ;

# if STDIN is undef, user pressed Ctrl-Z
last GET_ITEM_CHOICES unless defined $anything;

# $anything should be all digits
chomp( $anything );

# validate the user's choice
if( looks_like_number( $anything )
&& $anything >= 1
&& $anything <= @menu
){

# it's a valid number
my $number = $anything - 1;

# has it been enter before?
if( exists $choice{ $number } ){
warn "\n\n***** '$anything' is already choosen *****\n\n\n";
}else{
$choice{ $number } = 1;
}

}else{ # not a valid number
warn "\n\n***** '$anything' is not a numbered item in the list
*****\n\n\n";
}
}

# make output more readable
print "\n\n\n";

# return choices sorted as numbers
return sort { $a <=> $b } keys %choice;
}

# list of names for testing
my @names = qw/ fred betty barney dino wilma pebbles bamm-bamm /;

# call the sub
my @choosen = choose_from_menu( @names );

# show the results
print "names choosen: @names[@choosen]\n";

__END__




--
Just my 0.00000002 million dollars worth,
Shawn

Confusion is the first step of understanding.

Programming is as much about organization and communication
as it is about coding.

The secret to great software: Fail early & often.

Eliminate software piracy: use only FLOSS.

"Make something worthwhile." -- Dear Hunter

--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/