Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Re: python like named placeholders sprintf ?

by Jenda (Abbot)
on Jan 15, 2009 at 22:24 UTC ( [id://736697]=note: print w/replies, xml ) Need Help??


in reply to python like named placeholders sprintf ?

use Interpolation '%:$$->$' => sub {sprintf '%'.$_[0], $_[1]}; my $x = 41.758; my $name = "Jenda"; print "Hello $%{'06d'}{$x} or $%{' 10.5f'}{$x}, $%{'20s'}{$name}, and +so forth\n"; $x = 1.7; $name = "Leaveolus"; print "Hello $%{'06d'}{$x} or $%{' 10.5f'}{$x}, $%{'20s'}{$name}, and +so forth\n";

The need for the singlequotes inside the curlies makes it a little less nice, but I do not see a way around that. OTOH, you can add your own formats by modifying the subroutine in that use statement. Or you can define different interpolations if you like:

use Interpolation 'S' => sub { local $_ = sprintf("%.2f", shift()); 1 while s/^(-?\d+)(\d{3})/$1,$2/; '$'.$_; }; #... print "And the total price is $S{$Price}.\n";

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (8)
As of 2024-04-16 11:53 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found