Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Re: fixed length string

by Crian (Curate)
on Jul 05, 2004 at 09:43 UTC ( [id://371833]=note: print w/replies, xml ) Need Help??


in reply to fixed length string

You could use a function like this:

#!/usr/bin/perl use strict; use warnings; sub cut_fill ($$); my $word = 'someword'; print "'", cut_fill($word, 5), "'\n"; print "'", cut_fill($word, 10), "'\n"; sub cut_fill ($$) { my ($word, $length) = @_; if (length($word) >= $length) { $word = substr($word, 0, $length); } else { $word .= ' 'x($length - length($word)); } return $word; }
Update: Or you can use sprintf of corse, as shown in the solutions below. But if you need something more special, a function like this will be handy.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (5)
As of 2024-04-19 23:16 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found