For Programmers: Free Programming Magazines  


Home > Archive > PERL Miscellaneous > April 2005 > compound conditional









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 compound conditional
Bob

2005-04-24, 3:56 pm

Can I create a compound conditional with the :? shortform?
I tried putting two statements in curly brackets but that didn't work.

I ended up using if-then but I didn't want to take up much source room
(it only sets a variable to "is" or "are" text :-)).
Tad McClellan

2005-04-24, 3:56 pm

Bob <ask@me.com> wrote:

> Can I create a compound conditional with the :? shortform?



Yes.


> I tried



If you show your code we can help fix your code.

If you don't, we can't.


> putting two statements in curly brackets but that didn't work.



You can't just make shit up and expect the computer to know what you mean.


> I ended up using if-then but I didn't want to take up much source room
> (it only sets a variable to "is" or "are" text :-)).



my $to_be = singular($txt) ? 'is' : 'are';


--
Tad McClellan SGML consulting
tadmc@augustmail.com Perl programming
Fort Worth, Texas
Matija Papec

2005-04-24, 3:56 pm

X-Ftn-To: Bob

Bob <ask@me.com> wrote:
>Can I create a compound conditional with the :? shortform?
>I tried putting two statements in curly brackets but that didn't work.


Did you also tried ?:
? :)


--
Matija
Joe Smith

2005-04-25, 3:58 am

Bob wrote:
> Can I create a compound conditional with the :? shortform?


Yes, I can.

(home/jms) jms@mathras> cat test.pl
sub whichbit {
$num = shift;
$num & 0x80 ? 'bit 7' :
$num & 0x40 ? 'bit 6' :
$num & 0x20 ? 'bit 5' :
$num & 0x10 ? 'bit 4' :
$num & 0x08 ? 'bit 3' :
$num & 0x04 ? 'bit 2' :
$num & 0x02 ? 'bit 1' :
$num & 0x01 ? 'bit 0' :
'none';
}

foreach (0 .. 17) {
print "The highest bit set in $_ is ",whichbit($_),"\n";
}

(home/jms) jms@mathras> perl test.pl
The highest bit set in 0 is none
The highest bit set in 1 is bit 0
The highest bit set in 2 is bit 1
The highest bit set in 3 is bit 1
The highest bit set in 4 is bit 2
The highest bit set in 5 is bit 2
The highest bit set in 6 is bit 2
The highest bit set in 7 is bit 2
The highest bit set in 8 is bit 3
The highest bit set in 9 is bit 3
The highest bit set in 10 is bit 3
The highest bit set in 11 is bit 3
The highest bit set in 12 is bit 3
The highest bit set in 13 is bit 3
The highest bit set in 14 is bit 3
The highest bit set in 15 is bit 3
The highest bit set in 16 is bit 4
The highest bit set in 17 is bit 4

-Joe
Sponsored Links







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

Copyright 2008 codecomments.com