Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

one-line conditional print?

by MrSnrub (Beadle)
on May 16, 2011 at 23:13 UTC ( [id://905168]=perlquestion: print w/replies, xml ) Need Help??

MrSnrub has asked for the wisdom of the Perl Monks concerning the following question:

Is there a way to print a section of a print statement only if a certain condition is true and all within that same print statement? e.g.

print "There " . ($count == 1 ? " is " : " are " ) . " $count piece" . ($count == 1 ? "s" : "" ) . " of pizza left.\n";

Is this possible? Or do I have to go with an if, an else, and two print statements?

Replies are listed 'Best First'.
Re: one-line conditional print?
by BrowserUk (Patriarch) on May 16, 2011 at 23:22 UTC

    One of several ways:

    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.
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!

    Cheers Rolf

    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 ...

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.
      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)

        Those are often called slices nonetheless.

        Or maybe we need an extra conditional :)

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.
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?
Re: one-line conditional print?
by ww (Archbishop) on May 17, 2011 at 00:19 UTC
    YAW (yet another way):
    #!/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
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?

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";

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://905168]
Approved by ikegami
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others scrutinizing the Monastery: (3)
As of 2024-04-24 05:33 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found