Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer

Printf : How to repeat characters?

by westrock2000 (Beadle)
on Oct 03, 2010 at 04:22 UTC ( #863137=perlquestion: print w/replies, xml ) Need Help??

westrock2000 has asked for the wisdom of the Perl Monks concerning the following question:

I would like to use printf, but would also like to print a character multiple times. I cannot figure out the syntax

I have figured out you can do something like
printf ("%c" x 3,65,66,67);
Which will print ABC
But I what I would like to be able to do is something like AABBBCCCC

Normal print would just be as follows, but the commas throw me for a loop when it comes to printf.
$a = chr(65); $b = chr(66); $c = chr(67); print ($a x 2, $b x 3, $c x 4);

Replies are listed 'Best First'.
Re: Printf : How to repeat characters?
by eyepopslikeamosquito (Chancellor) on Oct 03, 2010 at 04:28 UTC
      yep works perfect....the %s made all the difference :)
Re: Printf : How to repeat characters?
by james2vegas (Chaplain) on Oct 03, 2010 at 04:41 UTC
    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 \$
Re: Printf : How to repeat characters?
by AnomalousMonk (Bishop) on Oct 03, 2010 at 06:01 UTC

    Here's another approach that allows ordinary strings, formatted pairs, and formatted pairs with repeat counts to be freely intermixed. Not extensively tested, but seems to work with fair variety of things I've tried, and both with and without file handles. (The prototype is only needed if the function is defined after the point of invocation.) Note: Needs 5.10 for  // operator – easily replaced.

    >perl -wMstrict -le "sub fmtx (@); ; print STDERR fmtx [ '%3.6f', 1.23, 3 ], '---', [ '%0x', 10 ], [ '%s', 'hi', 4 ], qq{that's all, folks \n}, ; ; sub fmtx (@) { map ref() ? sprintf($_->[0],$_->[1]) x ($_->[2] // 1) : $_, @_ } " 2> fmtx_stderr >cat fmtx_stderr 1.2300001.2300001.230000---ahihihihithat's all, folks

    Update: Original example code did not actually work! Fixed – as far as I can tell. Caveat Programor.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://863137]
Approved by McDarren
Front-paged by sflitman
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (5)
As of 2019-10-19 04:57 GMT
Find Nodes?
    Voting Booth?