http://www.perlmonks.org?node_id=818707


in reply to Re: printf question
in thread printf question

almut's right.

You can get a slight improvement by keeping track of your longest word as you count them, though, and then using printf's "*" modifier to read the field width from that variable.

printf "%-*s %d\n", $maxLen, $word, $seen{$word};
I'd put a width specifier on the %d so the numbers line up nicely as well, but that's just my preference:
printf "%-*s %4d\n", $maxLen, $word, $seen{$word};

Mike