slok has asked for the wisdom of the Perl Monks concerning the following question:

I have 2 variables to output.
how can I format them such that the second variable will
always "print" starting from position 20?

Replies are listed 'Best First'.
Re: formatting output
by davorg (Chancellor) on Apr 24, 2001 at 19:31 UTC

    Do you mean somethnig like this?

    perl -e "printf '%-20s%s', 'one', 'two'"
    --
    <http://www.dave.org.uk>

    "Perl makes the fun jobs fun
    and the boring jobs bearable" - me

Re: formatting output
by ar0n (Priest) on Apr 24, 2001 at 19:31 UTC
    print $var1, " " x (20 - length($var1)), $var2;
    Untested and there's probably a printf statement to do the same...

    ar0n ]

Re: formatting output
by asiufy (Monk) on Apr 24, 2001 at 19:31 UTC
    Check out the format function, very powerful...
    perldoc -f format
Re: formatting output
by Anonymous Monk on Apr 24, 2001 at 20:41 UTC
    print pack("A20 A@", $s1, $s2);
      oops! That should have been '*'
      print pack("A20 A*", $s1, $s2);
        Slok:

        You could do better by using the ANSI codes to position
        your text anywhere on the screen. If you're running
        Windoze you'll need a file called ANSI.SYS
        Look up ANSI in CPAN - it's all there.

        Clachair
Re: formatting output
by husker (Chaplain) on Apr 24, 2001 at 23:01 UTC
    What behavior do you want if the first variable value is > 19 characters?

    printf is probably what you want.