Stop mail On 10/6/11, Chris Stinemetz <chrisstinem...@gmail.com> wrote: > trying to learn smart matching in an exercise. > > Why does this program output "odd" when I input an even number? > > Thank you, > > Chris > > #!/usr/bin/perl > > use warnings; > use strict; > > use 5.010; > > say "Checking the number <$ARGV[0]>"; > > my $favorite = 62; > > given( $ARGV[0] ) { > when( ! /^\d+$/ ) { say "Not a number!" } > > my @divisors = divisors( $ARGV[0] ); > > when( @divisors ~~ 2 ) { # 2 is in @divisors > say "$_ is even"; > continue; > } > > when( !( @divisors ~~ 2 ) ) { # 2 isn't in @divisors > say "$_ is odd!"; > continue; > } > > when( @divisors ~~ $favorite ) { > say "$_ is divisible by my favorite number"; > continue; > } > > when( $favorite ) { # $_ ~~ $favorite > say "$_ is my favorite number"; > continue; > } > > my @empty; > when( @divisors ~~ @empty ) { say "Number is prime" } > > default { say "$_ is divisible by @divisors" } > } > > sub divisors { > my $number = shift; > > my @divisors = (); > foreach my $divisor ( 2 .. ($ARGV[0]/2 + 1) ) { > push @divisors, $divisor unless $number % $divisor; > } > > return @divisors; > > -- > To unsubscribe, e-mail: beginners-unsubscr...@perl.org > For additional commands, e-mail: beginners-h...@perl.org > http://learn.perl.org/ > > >
-- Sent from my mobile device -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/