Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Re: Printf : How to repeat characters?

by james2vegas (Chaplain)
on Oct 03, 2010 at 04:41 UTC ( #863142=note: print w/ replies, xml ) Need Help??


in reply to Printf : How to repeat characters?

This also works, and doesn't require duplicating parameters passed to print / printf:

use strict; use warnings; my $a = chr(65); my $b = chr(66); my $c = chr(67); printf ('%1$s'x2 . '%2$s'x3 . '%3$s'x4, $a, $b, $c);
The %number$ modifier allows you to use a specific parameter passed to printf (see sprintf for more details), so we create a format string that prints the 1st parameter 2 times, the second parameter 3 times and the 3rd parameter 4 times, or '%1$s%1$s%2$s%2$s%2$s%3$s%3$s%3$s%3$s' though bear in mind if you use an interpolating string ("..." or qq{...}) you would likely need to escape the $ to \$


Comment on Re: Printf : How to repeat characters?
Select or Download Code

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (8)
As of 2014-07-24 09:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (159 votes), past polls