Re: one-line conditional print?
by BrowserUk (Patriarch) on May 16, 2011 at 23:22 UTC
|
printf "There %s $_ piece%s of pizza left\n",
$_==1 ? ('is','') : ('are','s')
for 0 .. 2;;
There are 0 pieces of pizza left
There is 1 piece of pizza left
There are 2 pieces of pizza left
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.
| [reply] [d/l] |
Re: one-line conditional print?
by LanX (Saint) on May 16, 2011 at 23:32 UTC
|
if you want more concise code you could take advantage of the fact that while interpolating arrays/hashes indices/keys are evaluated.¹
sub pl { $count == 1 ? 0 :1 } # check plural
my @s=("","s");
my @are=qw/is are/;
for $count (1..3) {
print "There $are[pl] $count piece$s[pl] of pizza left!\n";
}
gives
There is 1 piece of pizza left!
There are 2 pieces of pizza left!
There are 3 pieces of pizza left!
This approach is quite flexible if you need to handle irregular plurals like @children=qw(child children)
HTH!
PS: if you wanna get rid of the pl() function you could use tied arrays evaluating index $count.
UPDATE: cleaned code...
1) yes eval is not the only backdoor for code injection ... | [reply] [d/l] [select] |
Re: one-line conditional print?
by ikegami (Patriarch) on May 16, 2011 at 23:15 UTC
|
That's works as is, although pizzas are usually divided into slices rather than pieces.
| [reply] |
|
That's works as is, although pizzas are usually divided into slices rather than pieces.
This falls apart quickly past 22 inches, too much cardboard wasted on a round pizza, the pizza turns square, and so do the slices. More than 22 inches you say, its family/party size (think 6+ adults)
| [reply] |
|
| [reply] |
Re: one-line conditional print?
by sundialsvc4 (Abbot) on May 17, 2011 at 00:13 UTC
|
Consider Text::Pluralize.
print pluralize "There (is|are) (no|one|%d) slice(s) of pizza left.", $count);
Q.E.D.
Note that this strategy, “slick” though it is, may or may not be appropriate for languages other than English. It is not a general “I18N” solution.
| |
|
Interesting module (it's pointers like this that keep me hanging around the monastery). Tiny quibble: you need curlies to have a 0-based index.
print pluralize "There (is|are) (no|one|%d) slice(s) of pizza left.",
+0;
There are 0 slices of pizza left.
print pluralize "There (is|are) (no|one|%d) slice(s) of pizza left.",
+1
There is no slice of pizza left.
print pluralize "There (is|are) {no|one|%d} slice(s) of pizza left.",
+0;
There are no slices of pizza left.
| [reply] [d/l] |
Re: one-line conditional print?
by JavaFan (Canon) on May 17, 2011 at 06:47 UTC
|
print "There " . ($count == 1 ? " is " : " are " ) . " $count piece" . ($count == 1 ? "s" : "" ) . " of pizza left.\n";
Is this possible?
I got to ask. Given that you already have a line of code, what makes you think it's easier to ask on Perlmonks whether than line of code works, than to run it?
| [reply] [d/l] |
Re: one-line conditional print?
by ww (Archbishop) on May 17, 2011 at 00:19 UTC
|
#!/usr/bin/perl
use strict;
use warnings;
use 5.012;
# 905168
for ( my $count = 0; $count <=5; $count++ ) {
say "There " . ($count == 1 ? " is " : " are " ) . " $count piece"
+ . ($count != 1 ? "s" : "" ) . " of pizza left.";
}
=prints
There are 0 pieces of pizza left.
There is 1 piece of pizza left.
There are 2 pieces of pizza left.
There are 3 pieces of pizza left.
There are 4 pieces of pizza left.
There are 5 pieces of pizza left.
=cut
| [reply] [d/l] |
Re: one-line conditional print?
by AnomalousMonk (Archbishop) on May 17, 2011 at 03:07 UTC
|
MrSnrub: Just out of curiosity – what happened when you tried the code of the OP? You did try it yourself, didn't you?
| [reply] |
Re: one-line conditional print?
by jwkrahn (Abbot) on May 17, 2011 at 03:01 UTC
|
print "There ", $count == 1 ? " is 1 piece" : " are $count pieces", "
+of pizza left.\n";
| [reply] [d/l] |