in reply to What does the ternary statement actually do?

Suitably described above. There is one handy case not mentioned as I begin to type this

$bobscats=2; $billscats=1; printf "Bob has %s\n", catstat($bobscats); printf "Bill has %s\n", catstat($billscats); sub catstat { my($count)=@_; sprintf "%d %s", $count, $count>1?'cats':'cat'; }

will print out Bob has 2 cats
Bill has 1 cat
So that it doesn't appear that you aren't sure how to say things like xxx has 1 cat(s)

Replies are listed 'Best First'.
Re^2: What does the ternary statement actually do?
by tadman (Prior) on Aug 13, 2001 at 07:18 UTC
    In C mode, that's what I usually do, only slightly different:
    printf ("%d %s%s\n", $count, "cat", ($count==1)?"":"s"); printf ("%d %s%s\n", $count, "cact", ($count==1)?"us":"i"); printf ("%d %s%s\n", $count, "Elvi", ($count==1)?"s":"i");
    As an alternative, you might want to use:
    print $count, "cat", (($count==1)?"":"s"), "\n"; print $count, "cact", (($count==1)?"us":"i"), "\n"; print $count, "Elvi", (($count==1)?"s":"i"), "\n";
    Or perhaps as a method:
    sub quantize { my ($quantity, $singular, $plural) = @_; return (1==$quantity)? $singular : $plural; } $count = 2; print "$count @{[quantize($count,'cat','cats')]}\n";