#!/usr/local/bin/perl -c
^ here?
Main Topics
Browse All TopicsI saw something about switch statements, so I tried one. But it's not working. Do you know what's wrong?
chomp( my $score = <STDIN> );
switch( $score )
{
case [90..100] { print "\n\nYou have an A!\n\n;" }
case [80..89] { print "\n\nYou have an B!\n\n;" }
case [70..79] { print "\n\nYou have an C!\n\n;" }
case [60..69] { print "\n\nYou have an D!\n\n;" }
case [0..59] { print "\n\nYou have an F!\n\n;" }
else { print "\n\nError: scores may not be less than 0 or greater than 100.\n\n"; }
}
Any help appreciated. Thank you.
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
use Switch;
chomp( my $score = <STDIN> );
switch( $score ) {
case [90..100] { print "\n\nYou have an A!\n\n;" }
case [80..89] { print "\n\nYou have an B!\n\n;" }
case [70..79] { print "\n\nYou have an C!\n\n;" }
case [60..69] { print "\n\nYou have an D!\n\n;" }
case [0..59] { print "\n\nYou have an F!\n\n;" }
else { print "\n\nError: scores may not be less than 0 or greater than 100.\n\n"; }
}
#!/usr/local/bin/perl
use Switch;
chomp( my $score = <STDIN> );
switch( $score )
{
case [90..100] { print "\n\nYou have an A!\n\n;" }
case [80..89] { print "\n\nYou have an B!\n\n;" }
case [70..79] { print "\n\nYou have an C!\n\n;" }
case [60..69] { print "\n\nYou have an D!\n\n;" }
case [0..59] { print "\n\nYou have an F!\n\n;" }
else { print "\n\nError: scores may not be less than 0 or greater than 100.\n\n"; }
}
This is perl, version 5.005_03 built for sun4-solaris
Copyright 1987-1999, Larry Wall
Perl may be copied only under the terms of either the Artistic License or the
GNU General Public License, which may be found in the Perl 5.0 source kit.
Complete documentation for Perl, including FAQ lists, should be found on
this system using `man perl' or `perldoc perl'. If you have access to the
Internet, point your browser at http://www.perl.com/, the Perl Home Page.
I would do
sub in {
my $elem = shift;
foreach my $value (@_) {
return 1 if $value eq $elem;
}
return 0;
}
chomp( my $score = <STDIN> );
if (in($score=>(90..100))) {
print "\n\nYou have an A!\n\n;"
} elsif (in($score=>(80..89))) {
print "\n\nYou have an B!\n\n;"
} elsif (in($score=>(70..79))) {
print "\n\nYou have an C!\n\n;"
} elsif (in($score=>(60..69))) {
print "\n\nYou have an D!\n\n;"
} elsif (in($score=>(0..59))) {
print "\n\nYou have an E!\n\n;"
} else {
print "\n\nError: scores may not be less than 0 or greater than 100.\n\n";
}
perl grades.pl 50.2
You have an A!
Uh, oh! That's not right...
chomp( my $score = <STDIN> );
for ($score) {
if (90 >= $score && $score <= 100) { print "\n\nYou have an A!\n\n"; }
elsif (80 >= $score && $score <= 89) { print "\n\nYou have an B!\n\n"; }
elsif (70 >= $score && $score <= 79) { print "\n\nYou have an C!\n\n"; }
elsif (60 >= $score && $score <= 69) { print "\n\nYou have an D!\n\n"; }
elsif (0 >= $score && $score <= 59) { print "\n\nYou have an F!\n\n"; }
else { print "\n\nError: scores may not be less than 0 or greater than 100.\n\n"; }
}
if (90 <= $score && $score <= 100) { print "\n\nYou have an A!\n\n"; }
elsif (80 <= $score && $score < 90) { print "\n\nYou have an B!\n\n"; }
elsif (70 <= $score && $score < 80) { print "\n\nYou have an C!\n\n"; }
elsif (60 <= $score && $score < 70) { print "\n\nYou have an D!\n\n"; }
elsif (0 <= $score && $score < 60) { print "\n\nYou have an F!\n\n"; }
else { print "\n\nError: scores may not be less than 0 or greater than 100.\n\n"; }
if( my $grade = $in{int $score} ){
if (90 <= $score and $score <= 100) { print "\n\nYou have an A!\n\n"; }
elsif (80 <= $score and $score < 90) { print "\n\nYou have an B!\n\n"; }
elsif (70 <= $score and $score < 80) { print "\n\nYou have an C!\n\n"; }
elsif (60 <= $score and $score < 70) { print "\n\nYou have an D!\n\n"; }
elsif (0 <= $score and $score < 60) { print "\n\nYou have an F!\n\n"; }
else { print "\n\nError: scores may not be less than 0 or greater than 100.\n\n"; }
Ah, $score = $ARGV[0]; fixed it.
chomp( my $score = <STDIN> );
$score = $ARGV[0];
for ($score) {
if (90 <= $score and $score <= 100) { print "\n\nYou have an A!\n\n"; }
elsif (80 <= $score and $score < 90) { print "\n\nYou have an B!\n\n"; }
elsif (70 <= $score and $score < 80) { print "\n\nYou have an C!\n\n"; }
elsif (60 <= $score and $score < 70) { print "\n\nYou have an D!\n\n"; }
elsif (0 <= $score and $score < 60) { print "\n\nYou have an F!\n\n"; }
else { print "\n\nError: scores may not be less than 0 or greater than 100.\n\n"; }
}
Does this look good?
my $score = $ARGV[0];
for ($score) {
if (90 <= $score and $score <= 100) { print "\n\nYou have an A!\n\n"; }
elsif (80 <= $score and $score < 90) { print "\n\nYou have an B!\n\n"; }
elsif (70 <= $score and $score < 80) { print "\n\nYou have an C!\n\n"; }
elsif (60 <= $score and $score < 70) { print "\n\nYou have an D!\n\n"; }
elsif (0 <= $score and $score < 60) { print "\n\nYou have an F!\n\n"; }
else { print "\n\nError: scores may not be less than 0 or greater than 100.\n\n"; }
}
you are correct
my $score = $ARGV[0];
if (90 <= $score and $score <= 100) { print "\n\nYou have an A!\n\n"; }
elsif (80 <= $score and $score < 90) { print "\n\nYou have an B!\n\n"; }
elsif (70 <= $score and $score < 80) { print "\n\nYou have an C!\n\n"; }
elsif (60 <= $score and $score < 70) { print "\n\nYou have an D!\n\n"; }
elsif (0 <= $score and $score < 60) { print "\n\nYou have an F!\n\n"; }
else { print "\n\nError: scores may not be less than 0 or greater than 100.\n\n"; }
If you're going to use
for ($score) {
then you can say
for( $score ){
if (90 <= $_ && $_ <= 100) { print "\n\nYou have an A!\n\n"; }
elsif (80 <= $_ && $_ < 90) { print "\n\nYou have an B!\n\n"; }
elsif (70 <= $_ && $_ < 80) { print "\n\nYou have an C!\n\n"; }
elsif (60 <= $_ && $_ < 70) { print "\n\nYou have an D!\n\n"; }
elsif (0 <= $_ && $_ < 60) { print "\n\nYou have an F!\n\n"; }
else { print "\n\nError: scores may not be less than 0 or greater than 100.\n\n"; }
}
Or
my $score = $ARGV[0];
if (90 <= $score and $score <= 100) { print "\n\nYou have an A!\n\n"; }
elsif (80 <= $score) { print "\n\nYou have an B!\n\n"; }
elsif (70 <= $score) { print "\n\nYou have an C!\n\n"; }
elsif (60 <= $score) { print "\n\nYou have an D!\n\n"; }
elsif (0 <= $score) { print "\n\nYou have an F!\n\n"; }
else { print "\n\nError: scores may not be less than 0 or greater than 100.\n\n"; }
my $score = $ARGV[0];
if (100 < $score ) { print "\n\nError: scores may not be greater than 100.\n\n"; }
elsif (90 <= $score) { print "\n\nYou have an A!\n\n"; }
elsif (80 <= $score) { print "\n\nYou have an B!\n\n"; }
elsif (70 <= $score) { print "\n\nYou have an C!\n\n"; }
elsif (60 <= $score) { print "\n\nYou have an D!\n\n"; }
elsif (0 <= $score) { print "\n\nYou have an F!\n\n"; }
else { print "\n\nError: scores may not be less than 0.\n\n"; }
Business Accounts
Answer for Membership
by: oleberPosted on 2005-11-29 at 23:53:50ID: 15386252
you need to import the Module Switch