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

I have strings that look like this:
1.Company_name some_value1 some_value2
2.Company_name some_value1 some_value2
and I want the values to line up under eachother. The company name and values are variable length strings.
How would I use sprinf to line these values up in columns?

Replies are listed 'Best First'.
Re: sprintf formatting
by Marshall (Canon) on Jul 30, 2009 at 17:04 UTC
    Look at: http://perldoc.perl.org/functions/sprintf.html and consult your Perl manual about sprintf. Take a stab at it and let us know how it goes. The spec shows how to create fields of a certain number of characters and how to left or right justify things within those fields.
Re: sprintf formatting
by ikegami (Patriarch) on Jul 30, 2009 at 17:16 UTC
    my $fmt = "%-${max_company_name_len}s %-${max_value1_len}s %s\n";

      Or perhaps:

      sprintf "%-*s %-*s %s\n", $max_company_name_len, $company_name, $max_s +ome_value1_len, $some_value1, $some_value2;
Re: sprintf formatting
by toolic (Bishop) on Jul 30, 2009 at 17:16 UTC
    The CPAN module Text::Table will handle variable length strings for you. It is an alternative to using sprintf.