Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Re: printf question

by almut (Canon)
on Jan 21, 2010 at 10:49 UTC ( [id://818683]=note: print w/replies, xml ) Need Help??


in reply to printf question

Sure. Something like this

printf "%-33s %d\n", $word, $seen{$word};

The 33 sets the width of the field, and the minus sign left-aligns the content in it.

Replies are listed 'Best First'.
Re^2: printf question
by RMGir (Prior) on Jan 21, 2010 at 12:53 UTC
    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
Re^2: printf question
by peokai (Novice) on Jan 21, 2010 at 10:51 UTC
    Thank you good Sir!

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://818683]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (5)
As of 2024-04-24 01:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found