Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

printf x format ?

by pgmer6809 (Sexton)
on Mar 02, 2022 at 05:47 UTC ( #11141744=perlquestion: print w/replies, xml ) Need Help??

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

The only format that seems to work in printf (for printing a character) is the %s one. what is wrong with this:

$ca = 'A'; print "$ca\n"; # outputs A printf "%s\n", $ca ; #outputs A printf "%c\n", $ca ; # outputs blank printf "%x\n", $ca ; #outputs 0 (zero) printf "%d\n", $ca ; #outputs 0 (zero)

Replies are listed 'Best First'.
Re: printf x format ?
by Fletch (Bishop) on Mar 02, 2022 at 05:51 UTC

    The string "A" isn't a number so none of the number or character expecting formats will do what you're apparently expecting (when converted to a number the numeric value is zero). Pass a number if you want to format a number (chr and/or ord will probably be of interest).

    $ perl -E '$x = ord(q{A});printf( join( qq{\t}, qw( %c %x %d ) ) . qq{ +\n}, ($x)x3 )' A 41 65

    The cake is a lie.
    The cake is a lie.
    The cake is a lie.

Re: printf x format ?
by eyepopslikeamosquito (Bishop) on Mar 02, 2022 at 11:47 UTC

    Perl is not C. Though I occasionally use sprintf in Perl, I rarely use printf, whose doco warns:

    Don't fall into the trap of using a printf when a simple print would do. The print is more efficient and less error prone.

    Hope the following test program clarifies:

    use strict; use warnings; my $ca = 'A'; print "$ca\n"; # outputs A printf "%s\n", $ca; # outputs A printf "%c\n", ord($ca); # outputs A printf "%x\n", ord($ca); # outputs 41 printf "%d\n", ord($ca); # outputs 65

    See also: ord, chr, hex, oct, sprintf

      Thanks. That clears it up nicely. I read the bit about not using printf when print will do, but I did not know to look for ord. I was hoping that by specifying $ca with single quotes, instead of double quotes Perl would not treat $ca as a string. As the other reply hinted, I have been doing C recently, and Perl is not C. Thanks again. pgmer6809

Re: printf x format ?
by ikegami (Patriarch) on Mar 03, 2022 at 07:19 UTC

    'A' produces a string. The same string as "A".

    The formats largely control how numbers are converted to strings for printing. Strings are already strings, so only one format is needed for them (%s).

    %c applies chr to a number, %x produces a hex representation of a number, and %d produces a decimal representation of a number.

    You probably wanted to pass ord('A') aka 0x41 aka 65.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (2)
As of 2022-11-26 09:27 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Notices?