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;

In reply to Re^2: Possible to use variable in printf statement? by GotToBTru
in thread Possible to use variable in printf statement? by mmartin

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.