Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number

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 the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (6)
As of 2016-10-22 03:14 GMT
Find Nodes?
    Voting Booth?
    How many different varieties (color, size, etc) of socks do you have in your sock drawer?

    Results (291 votes). Check out past polls.