1 Replies - 6154 Views - Last Post: 25 September 2013 - 02:27 AM

#1 amture106  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 159
  • Joined: 08-September 11

If condition question

Posted 25 September 2013 - 02:00 AM

Hello guys,

I'm trying to write a Perl program that takes votes from the user. It was going alright, until I needed to get input from the user and everything went downhill from there. My code is:
for ( my $x = 1 ; $x <= $#lead_actress_in_drama_series ; $x++ ) {
	print "[$x] $lead_actress_in_drama_series[$x] \n";
	$temp = $x;
}

print "\n\nPlease enter your choice for $lead_actress_in_drama_series[0] now: ";
$ch_f_dr = <>;#getting input from the user
chomp $ch_f_dr;#deleting the extra new line

if ($ch_f_dr ~~ (1..($temp-1)) {#* #checking if the user input is in the range or the array
		  print "Thank you for selecting $lead_actress_in_drama_series[$ch_f_dr] as $lead_actress_in_drama_series[0].\n\n";
		  push( @choices, $lead_actress_in_drama_series[$ch_f_dr] );#Get the user choice and put it in a new array
	}#* elsif ( $ch_f_dr ~~ $temp ) {#check if the user chose the last element of the array
		print "Please enter your write-in candidate: ";
		$temp = <>;#get the new option
		chomp $temp;
		print "Thank you for selecting $temp as $lead_actress_in_drama_series[0].\n\n";
                push( @choices, $temp );#add the user choice to a new array
	}#*
	else {
		while ($ch_f_dr !~ (1..$temp )) {#check if the user input isn't one of the option menu
			print "I'm sorry, but $ch_f_dr is not a valid option. \n";
 			print "Please enter your choice for $lead_actress_in_drama_series[0]  now: ";#get a new input
			$ch_f_dr = <>;
			chomp $ch_f_dr;
		}#*
	}


so I do have errors where * is.

Any ideas?

Is This A Good Question/Topic? 0
  • +

Replies To: If condition question

#2 Israel  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 7
  • View blog
  • Posts: 821
  • Joined: 22-November 04

Re: If condition question

Posted 25 September 2013 - 02:27 AM

I ran your code and these were the errors I received:

# perl ./d2.pl 
syntax error at ./d2.pl line 12, near ") {"
syntax error at ./d2.pl line 15, near "}"
Unmatched right curly bracket at ./d2.pl line 21, at end of line
syntax error at ./d2.pl line 21, near "}"
syntax error at ./d2.pl line 28, near "}"
Execution of ./d2.pl aborted due to compilation errors.


It seems some of the necessary {} where commented out and line 12 was missing a closing parenthesis.

This seems to run properly but you may need to remove or change #!/usr/bin/perl if you are on a different OS:

#!/usr/bin/perl

for ( my $x = 1 ; $x <= $#lead_actress_in_drama_series ; $x++ ) {
	print "[$x] $lead_actress_in_drama_series[$x] \n";
	$temp = $x;
}

print "\n\nPlease enter your choice for $lead_actress_in_drama_series[0] now: ";
$ch_f_dr = <>;#getting input from the user
chomp $ch_f_dr;#deleting the extra new line

if ($ch_f_dr ~~ (1..($temp-1))) { #* #checking if the user input is in the range or the array
		  print "Thank you for selecting $lead_actress_in_drama_series[$ch_f_dr] as $lead_actress_in_drama_series[0].\n\n";
		  push( @choices, $lead_actress_in_drama_series[$ch_f_dr] );#Get the user choice and put it in a new array
	} elsif ( $ch_f_dr ~~ $temp ) {#check if the user chose the last element of the array
		print "Please enter your write-in candidate: ";
		$temp = <>;#get the new option
		chomp $temp;
		print "Thank you for selecting $temp as $lead_actress_in_drama_series[0].\n\n";
                push( @choices, $temp );#add the user choice to a new array
	}
	else {
		while ($ch_f_dr !~ (1..$temp )) {#check if the user input isn't one of the option menu
			print "I'm sorry, but $ch_f_dr is not a valid option. \n";
 			print "Please enter your choice for $lead_actress_in_drama_series[0]  now: ";#get a new input
			$ch_f_dr = <>;
			chomp $ch_f_dr;
		}
	}


This was the output I received:

# perl ./d2.pl 


Please enter your choice for  now: 



I wasn't sure what to enter to test further.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1