Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Printf : How to repeat characters?

by westrock2000 (Sexton)
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);
Thanks

Comment on Printf : How to repeat characters?
Select or Download Code
Re: Printf : How to repeat characters?
by eyepopslikeamosquito (Canon) 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 (Monsignor) 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?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (6)
As of 2014-07-26 04:40 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (175 votes), past polls