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

I want to hold a varible printf format, I can do it C code, but unsure about PERL.
w = 5 p = 3 s = "abcdefg" printf "<%*.*s>\n", w, p, s
How can I do this in PERL??

Replies are listed 'Best First'.
Re: printf() formats
by sauoq (Abbot) on Sep 04, 2002 at 19:45 UTC

    Update: I guess I shouldn't have assumed it wouldn't work before providing an alternative. The direct translation should work fine:

    my $w = 5; my $p = 3; my $s = "abcdefg"; printf "<%*.*s>", $w,$p,$s;

    My original answer below is an alternative though.


    Something like the following will work for you:

    my $w = 5; my $p = 3; my $s = "abcdefg"; printf "<%$w.${p}s>\n", $s;
    Edit: added that ", $s" to the printf in the bottom example and added a quote in my first example. Thanks fglock++ and ichimunki++!
    -sauoq
    "My two cents aren't worth a dime.";
    
Re: printf() formats
by Ovid (Cardinal) on Sep 04, 2002 at 19:49 UTC

    Read perldoc -f sprintf for a complete description of the available formats.

    Cheers,
    Ovid

    Join the Perlmonks Setiathome Group or just click on the the link and check out our stats.

Re: printf() formats
by cLive ;-) (Prior) on Sep 05, 2002 at 08:10 UTC