For Programmers: Free Programming Magazines  


Home > Archive > PERL Programming > March 2004 > alternation operator question









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author alternation operator question
Steven Post

2004-03-18, 6:46 pm

Xref: kermit alt.perl:26377

I probably just need to take a break and the problem will suddenly
become clear, but...

I'm writing some code that contains an alternation operator, and it is
working exactly the opposite of what I would expect. I'm probably
missing something obvious, but you guys take a look.

These two examples are producing diametrically opposite results:

ex. 1

$gender == 'male' ? $genderCode = 1 : $genderCode = 2;


ex. 2

if ($gender == 'male') { $genderCode = 1 }
else { $genderCode = 2 }

What did I miss?

sp
Beable van Polasm

2004-03-18, 6:46 pm

Steven Post <spost@whitehouse.gov> writes:
>
> These two examples are producing diametrically opposite results:
>
> ex. 1
>
> $gender == 'male' ? $genderCode = 1 : $genderCode = 2;


AHEM!!! Please read "perldoc perlop" and search for "Equality Operators".

> ex. 2
>
> if ($gender == 'male') { $genderCode = 1 }
> else { $genderCode = 2 }
>
> What did I miss?


You failed to read "perldoc perlop".



"==" is for numbers, "eq" is for strings


--
Juan Antonio Samaranch: Aussie, Aussie, Aussie.
Audience: OI! OI! OI!
Juan Antonio Samaranch: Well done.
http://beable.com
Steven Post

2004-03-18, 6:46 pm

In article <736fjte.fsf@dingo.beable.com>,
Beable van Polasm <beable+unsenet@beable.com.invalid> wrote:

> Steven Post <spost@whitehouse.gov> writes:
>
> AHEM!!! Please read "perldoc perlop" and search for "Equality Operators".
>
>
> You failed to read "perldoc perlop".
>
>
>
> "==" is for numbers, "eq" is for strings


Duh. Thanks.

This is why extreme programming is worth a little consideration. I have
too much stuff to pack into one brain.
Gunnar Hjalmarsson

2004-03-18, 6:46 pm

Beable van Polasm wrote:
> Steven Post <spost@whitehouse.gov> writes:
>
> You failed to read "perldoc perlop".
>
>
>
> "==" is for numbers, "eq" is for strings


Which Perl would have indicated if warnings had been enabled.
Accordingly, not using warnings was Steven's second mistake. ;-)

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl

Beable van Polasm

2004-03-18, 6:46 pm

Gunnar Hjalmarsson <noreply@gunnar.cc> writes:

> Beable van Polasm wrote:
>
> Which Perl would have indicated if warnings had been
> enabled. Accordingly, not using warnings was Steven's second
> mistake. ;-)


Good point. And he's probably not using strict either. THREE STRIKES!
HE'S OUT! Oh the SHAME!


--
Do you realize you just spent a huge amount of time responding
(SERIOUSLY) to a joke? This could be an experiment by alien mind
control agents. -- Andrea Chen
http://beable.com/
Uri Guttman

2004-03-18, 6:46 pm

>>>>> "SP" == Steven Post <spost@whitehouse.gov> writes:

SP> In article <736fjte.fsf@dingo.beable.com>,
SP> Beable van Polasm <beable+unsenet@beable.com.invalid> wrote:
[color=darkred]

SP> Duh. Thanks.

SP> This is why extreme programming is worth a little consideration. I have
SP> too much stuff to pack into one brain.

and you still have a major bug in that line even if you fix the == to
eq. you are using ?: both incorrectly and backwards. it is meant to be
used to return an expression, not do side effect assignments.

uri

--
Uri Guttman ------ uri@stemsystems.com -------- http://www.stemsystems.com
--Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org
kz

2004-03-18, 6:46 pm

"Steven Post" <spost@whitehouse.gov> wrote in message
news:spost-BE81C4.19061001022004@netnews.comcast.net...

[snip]

> Duh. Thanks.
>
> This is why extreme programming is worth a little consideration. I have
> too much stuff to pack into one brain.


use strict; # lesson 1
use warnings; # lesson 2
my $gender;
my $genderCode;
$gender = 'male';
$genderCode = ($gender eq 'male')?1:2; # lesson 3
print "$gender $genderCode\n";

Did you try to achieve this?
You can do something like this as well:

use strict;
use warnings;
my $gender = 'male';
my $gC = {male => 1, female => 2};
print "$gender ",$gC->{$gender},"\n";

Further complicating the problem ;-) What would one have to do if depending
on a certain variable different subs need to be called? Would something like
this be feasible? How to make it more readable/simple/perlish?

use strict;
use warnings;
my $gender = 'male';
&{$gC2->{$gender}}($gender);
sub male { print "I am a ",shift,", my pseudonym is John Doe.\n";}
sub female { print "I am a ",shift,", my pseudonym is Jane Doe.\n";}

Best regards,

KZ


kz

2004-03-18, 6:46 pm

> "Steven Post" <spost@whitehouse.gov> wrote in message
> news:spost-BE81C4.19061001022004@netnews.comcast.net...
>
> [snip]
>
>
> use strict; # lesson 1
> use warnings; # lesson 2
> my $gender;
> my $genderCode;
> $gender = 'male';
> $genderCode = ($gender eq 'male')?1:2; # lesson 3
> print "$gender $genderCode\n";
>
> Did you try to achieve this?
> You can do something like this as well:
>
> use strict;
> use warnings;
> my $gender = 'male';
> my $gC = {male => 1, female => 2};
> print "$gender ",$gC->{$gender},"\n";
>
> Further complicating the problem ;-) What would one have to do if

depending
> on a certain variable different subs need to be called? Would something

like
> this be feasible? How to make it more readable/simple/perlish?
>

use strict;
use warnings;
my $gender = 'male';
my $gC2 = {male => \&male, female => \&female};
&{$gC2->{$gender}}($gender);
sub male { print "I am a ",shift,", my pseudonym is John Doe.\n";}
sub female { print "I am a ",shift,", my pseudonym is Jane Doe.\n";}

Best regards,

KZ


Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com