Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"

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

by GotToBTru (Prior)
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;

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://919321]
and not a whimper to be heard...

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (5)
As of 2018-03-21 03:57 GMT
Find Nodes?
    Voting Booth?
    When I think of a mole I think of:

    Results (263 votes). Check out past polls.