Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic

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?

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

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (14)
As of 2014-07-29 15:27 GMT
Find Nodes?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:

    Results (220 votes), past polls