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

Re: Printf : How to repeat characters?

by AnomalousMonk (Canon)
on Oct 03, 2010 at 06:01 UTC ( #863147=note: print w/replies, xml ) Need Help??


in reply to Printf : How to repeat characters?

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: note [id://863147]
help
Chatterbox?
and the fog begins to lift...

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (5)
As of 2017-02-28 01:17 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Before electricity was invented, what was the Electric Eel called?






    Results (394 votes). Check out past polls.