|
hi barry --
In a message dated 10/6/2006 7:31:10 P.M. Eastern Standard Time,
[EMAIL PROTECTED] writes:
> I guess this is a minor issue, but since the list has been quiet,
and
> this issue has been bothering me, here goes... > > When I need a string to be a certain length, either truncated or > right-padded with blanks, I've been doing this: > > $str = 'the quick gray fox...'; > $len = 25; # required string length. > if (length($str) >= $len) > { > $str = unpack("A".$len, $str); > } > else > { > $str = pack("A".$len, $str); > } > > ...and if I want it truncated of left-padded with blanks, I've been > doing this: > > $str = 'the quick gray fox...'; > $len = 25; # required string length. > if (length($str) >= $len) > { > $str = unpack("x".(length($str) - $len)." A".$len, $str); > } > else > { > $str = pack("x".($len - length($str))." A".length($str), $str); > } > > I've just had this nagging feeling that this method is overly complex and > there must be a more "Perl-ish" way of doing it. Anybody have any comments?? > > Barry Brevik don't know if this is particularly perl-ish since it is based on
the c/c++ printf()
and sprintf() format specification string (see any thorough discussion of
this for
more possible tricks), but here's one approach.
also, the term ``truncation'' is a bit ambiguous since it doesn't specify
whether
truncation is to be done on the right or left side.
for right-truncation and right-padding...
C:[EMAIL PROTECTED]>perl -we "use strict; my $len = 2; my
$str = 'the quick brown fox...';
$str = sprintf qq(%-*.*s), $len, $len, $str; print qq(<$str> \n);" <th> C:[EMAIL PROTECTED]>perl -we "use strict; my $len = 22; my $str =
'the quick brown fox...';
$str = sprintf qq(%-*.*s), $len, $len, $str; print qq(<$str> \n);" <the quick brown fox...> C:[EMAIL PROTECTED]>perl -we "use strict; my $len = 29; my $str =
'the quick brown fox...';
$str = sprintf qq(%-*.*s), $len, $len, $str; print qq(<$str> \n);" <the quick brown fox... > for right-truncation and left-padding...
$str = sprintf qq(%*.*s), $len, $len, $str; print qq(<$str> \n);" <th> C:[EMAIL PROTECTED]>perl -we "use strict; my $len = 22; my
$str = 'the quick brown fox...';
$str = sprintf qq(%*.*s), $len, $len, $str; print qq(<$str> \n);" <the quick brown fox...> C:[EMAIL PROTECTED]>perl -we "use strict; my $len = 29; my
$str = 'the quick brown fox...';
$str = sprintf qq(%*.*s), $len, $len, $str; print qq(<$str> \n);" < the quick brown fox...> for left-truncation and left-padding...
C:[EMAIL PROTECTED]>perl -we "use strict; my $len = 2; my
$str = 'the quick brown fox...';
$str = sprintf qq(%*.*s), $len, $len, substr($str, -$len); print qq(<$str> \n);" <..> C:[EMAIL PROTECTED]>perl -we "use strict; my $len = 5; my
$str = 'the quick brown fox...';
$str = sprintf qq(%*.*s), $len, $len, substr($str, -$len); print qq(<$str> \n);" <ox...> C:[EMAIL PROTECTED]>perl -we "use strict; my $len = 22; my $str =
'the quick brown fox...';
$str = sprintf qq(%*.*s), $len, $len, substr($str, -$len); print qq(<$str> \n);" <the quick brown fox...> C:[EMAIL PROTECTED]>perl -we "use strict; my $len = 29; my $str =
'the quick brown fox...';
$str = sprintf qq(%*.*s), $len, $len, substr($str, -$len); print qq(<$str> \n);" < the quick brown fox...> and i'll leave you to work out left-truncation and
right-padding. and, of course,
all of this could be encapsulated within a function.
hth -- bill walters
|
_______________________________________________ ActivePerl mailing list [email protected] To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
