Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Re: printf x format ?

by eyepopslikeamosquito (Archbishop)
on Mar 02, 2022 at 11:47 UTC ( [id://11141748]=note: print w/replies, xml ) Need Help??


in reply to printf x format ?

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

Replies are listed 'Best First'.
Re^2: printf x format ? -- solved
by pgmer6809 (Sexton) on Mar 03, 2022 at 03:30 UTC

    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

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others studying the Monastery: (2)
As of 2024-04-20 06:02 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found