Im trying to get my program to look like the following

Welcome to the 64th Primetime Emmy Awards!

The nominees for Outstanding Comedy Series are:
[1] Write In
[2] The Big Bang Theory, CBS
[3] Curb Your Enthusiasm, HBO
[4] Girls, HBO
[5] 30 Rock, NBC
[6] Veep, HBO
[7] Modern Family, ABC
Please enter your choice for Outstanding Comedy Series now: 0
I'm sorry, but 0 is not a valid option.
Please enter your choice for Outstanding Comedy Series now: 8
I'm sorry, but 8 is not a valid option.
Please enter your choice for Outstanding Comedy Series now: -1
I'm sorry, but -1 is not a valid option.
Please enter your choice for Outstanding Comedy Series now: 8
I'm sorry, but 8 is not a valid option.
Please enter your choice for Outstanding Comedy Series now: 2
Thank you for selecting The Big Bang Theory, CBS as Outstanding Comedy Series.

I can get the disply to work, but i can not figure outhow to convert a number from user input to apply to an item in my array. And how to validate that the number entered matches the number of items in the array.

Here is what I have so far

#!/usr/bin/perl
use warnings;
use strict; # required for our programs!
use diagnostics;

# Greeting
print "Welcome to the 64th Annual Primetime Emmy Awards!\n ";
print "\n";
print "=" x78;
print "\n";

# Start of nominee ballot section for Outstanding Comedy
print "The nominees for Outstanding Comedy Series are: \n\n";
print "\n";

    # Initiating array of comedy nominees
    my @comedy = ('Write In','The Big Bang Theory, CBS','Curb Your Enthusiasm, HBO',
                    'Girls, HBO','30 Rock, NBC','Veep, HBO','Modern Family, ABC');

        #Foreach to add numbers infront of choices
        my $c =1;
        for my $cshow (@comedy)
        {   
            $cshow = "  [$c] $cshow";
            $c += 1;
        }

        # $cshow is local variable of @comedy
        for my $cshow (@comedy)
        {
            print $cshow, "\n";
        }

    # Request for users vote
    print "\n"; 
    print "Please enter your choice for Outstanding Comedy Series now: \n";
    chomp (my $cpick = <>);
    if ($cpick ~~ @comedy)
    {
        print qq#Thank you for selecting, $cpick, as Outstanding Comedy Series!\n#;
    }

print "=" x78;
print "\n";

Recommended Answers

All 4 Replies

hai sateal8, use hash array instead of array.

hi sateal, here is the code. and dont't forget to mark solved

#!/usr/bin/perl
use 5.012;
say "Welcome to the 64th Annual Primetime Emmy Awards!\n";
say '=' x 78;
say "The nominees for Outstanding Comedy Series are: \n\n";
my %comedy=(1 => 'Write In', 2 => 'The Big Bang Theory, CBS', 3 => 'Curb Your Enthusiasm', 4 => 'HBO',
   5 =>  'Girls, HBO', 6 => '30 Rock, NBC', 7 => 'Veep, HBO', 8 => 'Modern Family, ABC');
    print "Please enter your choice for Outstanding Comedy Series now: \n";
    chomp (my $pick = <>);

if (exists $comedy{$pick}){
    say "Thank you for selecting $comedy{$pick} as Outstanding Comedy Series";
}
else {
    say "I\'m Sorry, but $pick is not a valid option.";
}

Thank you for the response but we havent covered hashes yet.

I am including a revised copy of code that still isnt working, i cant figure out how to match the number input to the array index

    #!/usr/bin/perl
    # Start of nominee ballot section for Outstanding Comedy
    print "The nominees for Outstanding Comedy Series are: \n\n";
    print "\n";

        # Initiating array of comedy nominees
        my @comedy = ('Write In','The Big Bang Theory, CBS','Curb Your Enthusiasm, HBO',
                        'Girls, HBO','30 Rock, NBC','Veep, HBO','Modern Family, ABC');

    #Foreach to add numbers infront of choices
            my $c =1;
            for my $cshow (@comedy)
            {   
                $cshow = "  [$c] $cshow";
                $c += 1;
            }

            # $cshow is local variable of @comedy
            for my $cshow (@comedy)
            {
                print $cshow, "\n";
            }
        # Request for users vote
        print "\n"; 
        print "Please enter your choice for Outstanding Comedy Series now: \n";
        chomp ( $answer = <> );

        if ($answer <= 0)
            {
                print "I'm sorry, but $answer is not a valid option.\n.\n";
                print "Please enter your choice for Outstanding Comedy Series now: \n";
                chomp ( $answer = <> );
            }
        if( $answer > 7)
            {
                print "I'm sorry, but $answer is not a valid option.\n";
                print "Please enter your choice for Outstanding Comedy Series now: \n";
                chomp ( $answer = <> );
            }

        if($answer = $array[$index])
            {
                print qq#Thank you for selecting, $answer, as Outstanding Comedy Series!\n#;
            }

Hi sateal8,
Below my comments is the modified script of yours that works, just as you wanted it. Before then please check the comments.

Thank you for the response but we havent covered hashes yet.

You may read it up.

... i cant figure out how to match the number input to the array index

There is no array defined as @array in your script, so why use " if( $answer = $array[$index] ){...} ", and what is $index?. Even your varaiable $answer was not defined. Please, use

use warnings;
use strict;

in your code. It will save you alot of troubles. Only use "use diagnostics;" for verbose information on errors.

Am sure you would have covered if/elsif/else and while loop. Simply use those to solve this problem.

We know that in Perl and most programming lang. array start from 0 not 1, but our script will show on the screen from 1, thanks to the variable $show_choices or $c as you called it. So, to get the array index inputted by the user, we get put the user's input into out array as array index and remove 1 from it since array really starts from 0. Then we can get the true position of our array index. That is the reason for:

... $comedy[$answer-1] ...

In your code, this:

...
$cshow = "  [$c] $cshow";  ## changes the array @comedy
...

changes the array @comedy, that each array index now have [number], in front of it. so it becomes another work altogether to display only the initial text of the array. So, to avoid that use this:

print "  [$c] $cshow\n";

instead, that is just display the new text without reassigning it to replace the original array index.

Please, always, Use <STDIN>, not just <>, except you really know what you want and what you are doing. :)!

lastly, Please always use meaningful variable names. It will help you understand what the variable is, when you come back to the script 6 months from now.

Below is the your revised copy of code, that I modified and it works fine.

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

# Start of nominee ballot section for Outstanding Comedy
print "The nominees for Outstanding Comedy Series are: \n\n";
print "\n";

# Initiating array of comedy nominees
my @comedy = (
    'Write In',
    'The Big Bang Theory, CBS',
    'Curb Your Enthusiasm, HBO',
    'Girls, HBO',
    '30 Rock, NBC',
    'Veep, HBO',
    'Modern Family, ABC',
);

#Foreach to add numbers infront of choices
my $show_choices = 1;
for my $cshow (@comedy) {
    print "  [$show_choices] $cshow\n";
    $show_choices += 1;
}

# Request for users vote
print "\n";

print "Please enter your choice for Outstanding Comedy Series now: ";
VOTE: while (1) {    # start an infinte loop
    chomp( my $answer = <STDIN> );

    if ( $answer <= 0 or $answer > 7 ) {  ## set the two boundries at onces
        print "I'm sorry, but $answer is not a valid option.\n\n";
        print "Please enter your choice for Outstanding Comedy Series now: ";
    }
    else {
        print
qq#Thank you for selecting, $comedy[$answer-1], as Outstanding Comedy Series!\n#;
        last VOTE;  ## exit the loop
    }
}

Hope this helps.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.