in reply to Formating a printf statement using a variable

I just love this place. I had a simliar issue and bam here is the answer on the search.

A differnt version of the question involved displaying columnized data.

If a word was larger then a certain size then I needed differnt columns and a print size. Using the offered method:

use strict; use warnings; my $fmt = "%-$print_size" . "s"; printf("$fmt","$_");

Thanks again and though it is late ++all

Replies are listed 'Best First'.
Re^2: Formating a printf statement using a variable
by Aristotle (Chancellor) on Jan 11, 2003 at 17:59 UTC
    Why not simply so? printf "%-${print_size}s", $_;

    Also note the lack of quotes around $_ - if you just refer to a variable by itself, don't interpolate it. There are cases where you need to do that, but you'll know them when you come across them. Most of the time it's just a wasted node in the optree and can even break code that would work fine without the quotes.

    HTH

    Makeshifts last the longest.

      Even better! Thank you! One less line and variable! I didn't know you could do that! ;-)

        Yep, very handy when you have something like my $type = $cond ? 'book' : 'module'; and want to print "books" or "modules": print "${type}s";. The straightforward print "$types"; will have strict complaining about an unknown $types.. although the curlies are very ugly extra linenoise, so in some cases (like this one) I'd prefer to say print $type . 's'; instead. But as with everything Perl: TMTOWTDI. :)

        Makeshifts last the longest.