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
_______________________________________________
ActivePerl mailing list
[email protected]
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs

Reply via email to