Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

warnings when using % and printf

by jeanluca (Deacon)
on May 30, 2006 at 17:57 UTC ( #552570=perlquestion: print w/replies, xml ) Need Help??

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

Dear Monks,

I just tried the following:
use warnings ; use strict ; my $total = 99 ; print "total is %s%\n", $total ;
Running this gives you/me the following warning:
Invalid conversion in printf: "%)" at /usr/lib/perl5/site_perl/5.8.7/M +y_module.pm line 401.
Any suggestions how to get rid of this warning?

Thnx a lot
Luca

Replies are listed 'Best First'.
Re: warnings when using % and printf
by Fletch (Bishop) on May 30, 2006 at 18:04 UTC

    The above mentioned ambiguities aside, you're probably trying to include a literal "%" in your output; if you read the docs for sprintf it shows you need to double it to get a single literal percent sign in the output.

Re: warnings when using % and printf
by VSarkiss (Monsignor) on May 30, 2006 at 18:01 UTC
Re: warnings when using % and printf
by swampyankee (Parson) on May 30, 2006 at 18:25 UTC

    Also, %s is to output a string. Given that Perl will convert variables to numbers or strings as needed, this may be a trivial gripe, but for a numerical value, such as 99, you probably want something like %d or %f vs %s.

    emc

    e(π√−1) = −1
Re: warnings when using % and printf
by HuckinFappy (Pilgrim) on May 30, 2006 at 19:07 UTC
Re: warnings when using % and printf
by jeanluca (Deacon) on May 30, 2006 at 18:25 UTC
    sorry, the print statement in my example should have been a printf!

    Thnx for the answers
    Luca
      Do you want %s%% so it prints a percent sign?
        yes!!
Re: warnings when using % and printf
by mikasue (Friar) on May 30, 2006 at 18:37 UTC
    Is %s% necessary? Why the second %?
      I need the % so I can print percentages!
A reply falls below the community's threshold of quality. You may see it by logging in.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (5)
As of 2023-09-29 11:16 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?