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

Maybe this question is way too simple, but the help will be greatly appreciated.

I want sprintf("%d %ss have been loaded", 13, "volume"); to return: "13 volumes have been loaded". In other words, I want to use "s" character after "%s" in sprintf(). How to do that?

Thanks to all!

V.Melnik

Replies are listed 'Best First'.
Re: sprintf("%ss", ...)
by AppleFritter (Vicar) on Jul 09, 2014 at 20:50 UTC

    That's working for me, as posted:

    say sprintf("%d %ss have been loaded", 13, "volume");

    produces

    13 volumes have been loaded

    Could you indicate what the problem you're experiencing is?

      Oops, sorry, it was a typo in other block ("." instead of ","). Thanks for helping me to wake up, it was an odd day. :)

      V.Melnik
        It happens. Glad to have been of help! *tips hat*
Re: sprintf("%ss", ...)
by Jim (Curate) on Jul 09, 2014 at 21:39 UTC

    Here's one way:

    my $message = sprintf('%d %s been loaded', $v, $v == 1 ? 'volume h +as' : 'volumes have')

    Here's a demonstration:

    C:\>perl -E "say sprintf('%d %s been loaded', $_, $_ == 1 ? 'volume ha +s' : 'volumes have') for 0 .. 9" 0 volumes have been loaded 1 volume has been loaded 2 volumes have been loaded 3 volumes have been loaded 4 volumes have been loaded 5 volumes have been loaded 6 volumes have been loaded 7 volumes have been loaded 8 volumes have been loaded 9 volumes have been loaded C:\>

    Alternatively (simpler):

    my $message = sprintf 'Volumes loaded: %d', $v;
Re: sprintf("%ss", ...)
by Laurent_R (Canon) on Jul 09, 2014 at 21:12 UTC
    Can't test it right now (in public transportations), but fairly sure it should work. Do you get any error message? Or is the output different from what you are lookig for? Or any other problem?