Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid

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 \$

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://863142]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (4)
As of 2017-12-17 07:10 GMT
Find Nodes?
    Voting Booth?
    What programming language do you hate the most?

    Results (462 votes). Check out past polls.