Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Re^2: Possible to use variable in printf statement?

by GotToBTru (Chaplain)
on Aug 08, 2011 at 20:16 UTC ( #919321=note: print w/ replies, xml ) Need Help??


in reply to Re: Possible to use variable in printf statement?
in thread Possible to use variable in printf statement?

It took a bit of looking. I went to printf (which directed me to sprintf) but I eventually found the syntax examples applicable to your purpose, dynamically sizing the output mask. We can't always get by with just scanning the docs. A function like printf has so many options, you really need to bite the bullet and slog your way through it, line by line, to find the particular one you need. If it isn't there, it's a fair indication you are using the wrong function. But I learned something from your question (and I am hopefully helping you as well).

It is up to you to decide which will be easier to decipher when you come back to this 6 months from now and can't recognize your own code ;) Do you want to (1) always supply the sizing variable as the first parameter or (2) do you want the flexibility to be able to use any parameter in the list?

Case 1:

printf OUTDATA "%-*s ",$varlength, $AoA[$x][$y];

Case 2: using the 3rd variable

printf OUTDATA "%-*3\$s %s %4\$s ",$AoA[$x][$y], $extravar, $varlength, $othervar;


Comment on Re^2: Possible to use variable in printf statement?
Select or Download Code

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (14)
As of 2014-09-23 12:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (221 votes), past polls