in reply to fixed length string
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.#!/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; }
|
|---|