in reply to Possible to use variable in printf statement?


Hey Guys, thanks for the replies.


toolic, ikegami, BrowserUK:
Thanks for the suggestions I will give them a try, but it looks like that is exactly what I need, Thanks!!!

JavaFan,
what one-liner? Sorry I must have missed something? Was that question for me?

AnomalousMonk,
I did read through the sprintf perldocs and did not find anything like what the users above had mentioned. i.e. ${x}
I did see though something like, ' *2$ ' but not the aforementioned one.


Thanks again,
Matt

.
  • Comment on Re: Possible to use variable in printf statement?

Replies are listed 'Best First'.
Re^2: Possible to use variable in printf statement?
by GotToBTru (Prior) on Aug 08, 2011 at 20:16 UTC

    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;