3 Replies - 6807 Views - Last Post: 19 September 2012 - 02:03 AM

#1 sateal8  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 17-September 12

apply number input to item in array

Posted 17 September 2012 - 11:45 AM

Im trying to get my program to look like and work 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 out how to convert a number from user input to apply to an item in my array. Or 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";



Is This A Good Question/Topic? 0
  • +

Replies To: apply number input to item in array

#2 dsherohman  Icon User is offline

  • Perl Parson
  • member icon

Reputation: 226
  • View blog
  • Posts: 654
  • Joined: 29-March 09

Re: apply number input to item in array

Posted 18 September 2012 - 02:19 AM

($cpick ~~ @comedy)

uses the "smart match" operator (~~) to try to find an item in @comedy which matches (as in either a regular expression match or an exact match) the value of $cpick. Try entering the text of one of the options (rather than its numeric index) and it should match.

To access an array element by its numeric index, you use $array[$index]. Note that you'll want to do some range checking first, as Perl treats negative array indexes as counting backwards from the end of the array - $array[-1] is the last element in the array, $array[-2] is the second-to-last, etc.
Was This Post Helpful? 0
  • +
  • -

#3 sateal8  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 17-September 12

Re: apply number input to item in array

Posted 18 September 2012 - 12:22 PM

OK Ive made a few changes and still feel really dumb that this isn't clicking, on how to tie a number input to an array value. It would be great if the name was entered but we are asked to use numbers. here is what i have changed.



    #!/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#;
    		
    		}




Was This Post Helpful? 0
  • +
  • -

#4 dsherohman  Icon User is offline

  • Perl Parson
  • member icon

Reputation: 226
  • View blog
  • Posts: 654
  • Joined: 29-March 09

Re: apply number input to item in array

Posted 19 September 2012 - 02:03 AM

The original posted version of your code had a comment saying that use strict; was required, but it seems to be missing from the revised version (and it doesn't compile under strict, either). I don't know why you took that out, but don't forget to put it back in! It's not just an arbitrary requirement for your class, it's a good practice that helps you write better code.

Anyhow, in my earlier response, I didn't mean that you should use $array[$index] literally. Replace "array" with the name of your array and "index" with the index of the item you want to access. For example, if you had the array @numbers, defined as:
my @numbers = ('Zero', 'One', 'Two', 'Three');


you would use $numbers[2] to get element number 2 (which is actually the third element, because arrays start counting from 0) out of it:
print $numbers[2];   # prints "Two"
$numbers[1] = "Uno"; # Array is now 'Zero', 'Uno', 'Two', 'Three'
for my $i (0 .. 3) { # Loop prints "ZeroUnoTwoThree"
  print $numbers[$i]
}


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1