1 Replies - 6082 Views - Last Post: 26 September 2013 - 02:08 AM

#1 amture106  Icon User is offline

  • D.I.C Head

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

Translation error

Posted 25 September 2013 - 07:55 PM

Hey guys,

So I'm pretty good in Java, but right now I'm learning Perl and when I apply Java logic I get so many error such as right now I'm trying to write a poll program so users would vote their favorite nominee. Any way here's the code:
#!/usr/bin/perl

#use Modern::Perl;
use strict;
use warnings;

my @lead_actress_in_drama_series = (
	"Outstanding Lead Actress in a Drama Series",
	"Vera Farmiga, Bates Motel",
	"Michelle Dockery, Downton Abbey",
	"Claire Danes, Homeland",
	"Robin Wright, House of Cards",
	"Elisabeth Moss, Mad Men",
	"Connie Britton, Nashville",
	"Kerry Washington, Scandal",
	"Write In"
);

my @choices;

print "Welcome to the 65th Primetie Emmy Awards! \n\n";
print
"============================================================================== \n\n";
print "The nominees for $lead_actress_in_drama_series[0] are: \n\n";

my (
	$temp,    $ch_f_dr, $ch_m_dr, $ch_dr, $ch_f_fu,
	$ch_m_fu, $ch_fu,   $ch_ho,   $ch_ot
);

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 = <>;
chomp $ch_f_dr;

if ($ch_f_dr == (1..($temp-1))) {#check if the user input is in one of the offered options
		  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] );
	} elsif ( $ch_f_dr == $temp ) {#check if the user want to enter their own entry
		print "Please enter your write-in candidate: ";
		$temp = <>;
		chomp $temp;
		print "Thank you for selecting $temp as $lead_actress_in_drama_series[0].\n\n";
		push( @choices, $temp );#place the enty in a new 
	}
	else {
		while ($ch_f_dr != (1..$temp )) {
			print "Stars loop\n\n";
			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: ";
			$ch_f_dr = <>;
			chomp $ch_f_dr;
			print "Ends loop \n";
		}
	}

#print the array to check entries. 
	for (@choices) {
		print;
		print "\n";
	}




I don't understand why my code will just go to the while loop with out checking first the number rang? or check the last element?

Please help.

Is This A Good Question/Topic? 0
  • +

Replies To: Translation error

#2 dsherohman  Icon User is offline

  • Perl Parson
  • member icon

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

Re: Translation error

Posted 26 September 2013 - 02:08 AM

In Perl, comparisons (and many other things) are all about context. When doing a comparison, the context is determined by the value to the left of the operator. Because of this, the comparisons in the lines
if ($ch_f_dr == (1 .. ($temp - 1)))
while ($ch_f_dr != (1 .. $temp)) {


are evaluated in scalar context. $ch_f_dr is on the left of the comparison and it's a scalar, so it imposes scalar context on the comparison.

So you're making a comparison in scalar context between a scalar and a list. When a list is evaluated in scalar context, its scalar value is the last item in the list. Therefore, those two lines are equivalent to
if ($ch_f_dr == $temp - 1)
while ($ch_f_dr != $temp) {



To achieve the results you want, you can either use the grep function:
if (grep /^$ch_f_dr$/, (1 .. ($temp - 1)))
while (!grep /^$ch_f_dr$/, (1 .. $temp)) {


or do it the old-fashioned way:
if ($ch_f_dr >= 1 && $ch_f_dr < $temp)
while ($ch_f_dr < 1 || $ch_f_dr >= $temp) {



(I see in your earlier question that you had at one point tried using the smart match operator (~~) to do this check for set membership, but recent versions of perl have rolled smart match back to "experimental" status and will issue warnings if you use it, so I would advise avoiding smart match until the issues surrounding it are worked out.)

As a side note, it drives me crazy whenever I see an unnecessary C-style for ( ; ; ) loop in Perl code. Try replacing your first loop with this instead:
for my $x (1 .. $#lead_actress_in_drama_series) {
  print "[$x] $lead_actress_in_drama_series[$x] \n";
}       
$temp = $#lead_actress_in_drama_series;


or even:
$temp = $#lead_actress_in_drama_series;
print "[$_] $lead_actress_in_drama_series[$_] \n" for 1 .. $temp;


Was This Post Helpful? 1
  • +
  • -

Page 1 of 1