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 ( [id://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?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (3)
As of 2024-04-26 08:27 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found