in reply to Re: I need a regExp to add spaces
in thread I need a regExp to add spaces

c,
$LimitValue = sprintf("%-1.1s", $LimitValue);
Is it possible in the above line of code to make the 1's into variables? eg.
$LimitValue = sprintf("%-$length.$lengths", $LimitValue);
Thanks in advance

Replies are listed 'Best First'.
Re^3: I need a regExp to add spaces
by beauregard (Monk) on Mar 21, 2005 at 17:23 UTC
    Of course. The obvious way:
    my $vlen = 1; $LimitValue = sprintf("%-${vlen}.${vlen}s", $LimitValue);

    The "real" sprintf to do variable field sizes is:

    $LimitValue = sprintf("%-*s", $vlen, $LimitValue);

    You can also do:

    $formatter = sprintf( "%%-%d.%ds", $vlen, $vlen ); $LimitValue = sprintf($formatter, $LimitValue);
    if you wanted to reuse the formatter elsewhere.

    c.