[EMAIL PROTECTED] <mailto:[EMAIL PROTECTED]>
wrote:
: Why doesn't this work to check the <STDIN> until it is one of the
: numbers "1 2 3 4 5" ?? I have tried multiple variations of this with
: different brackets ie. [ ] , and ( ) .
List::MoreUtils exports a function named 'any'. It will allow
you to set a condition using $_ for each value in the list.
use List::MoreUtils 'any';
my $type_number = 0;
until ( any { $type_number == $_ } 1 .. 5 ) {
A more straight forward, less flexible method involves using
the 'any' function from Quantum::Superpositions.
use Quantum::Superpositions 'any';
my $type_number = 0;
until ( $type_number == any( 1 .. 5 ) ) {
One advantage to this solution is the scalar result of 'any'.
Here is a very readable solution without comments.
use Quantum::Superpositions 'any';
my $valid_type = any( 1 .. 5 );
my $type = 0;
until ( $type == $valid_type ) {
A more generic solution might look like this.
#!/usr/bin/perl
use strict;
use warnings;
use Quantum::Superpositions 'any';
my $type_number = type_number(
[
'Virus',
'Spam',
'Hacking',
'Copyright',
'Child Pornography',
]
);
sub type_number {
my $types = shift;
my $valid_type = any( 1 .. @$types );
my @options = map "$_. $types->[ $_ - 1 ]\n", 1 .. @$types;
my $attempts = 0;
my $type_number = 0;
while (1) {
print
'Enter the number that corresponds to the type of',
" offense you would like a report for\n",
@options;
chomp( $type_number = <STDIN> );
return $type_number if $type_number == $valid_type;
last if ++$attempts > 10;
}
# Return undef on failure
# This could also be used to die with a usage message
return;
}
__END__
This makes it easy to add options. Try the above with this.
my $type_number = type_number(
[
'Virus',
'Spam',
'Hacking',
'Copyright',
'Child Pornography',
'Added option',
]
);
HTH,
Charles K. Clarkson
--
Mobile Homes Specialist
254 968-8328
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>