http://qs1969.pair.com?node_id=447910

kage-yojimbo has asked for the wisdom of the Perl Monks concerning the following question:

I have written this very primitive subroutine that pads a string with blanks to return a standard length string. I'm sure there is a much easier way to do this job - any suggestions?
Thanks!
sub standard_fill { my $string = $_[0]; my $length = $_[1]; for (my $i = length($string); $i <= $length; $i++) { $string = $string." "; } return $string; }

Replies are listed 'Best First'.
Re: Generating standard length strings
by Roy Johnson (Monsignor) on Apr 14, 2005 at 18:59 UTC
    sprintf "%-${length}s", $string" Update: note that length here is the name of a variable, not the function call being interpreted as a reference.

    Caution: Contents may have been coded under pressure.

      sprintf '%-*s', $length, $string

      Less noise in the format.

      ihb

      See perltoc if you don't know which perldoc to read!

Re: Generating standard length strings
by BrowserUk (Patriarch) on Apr 14, 2005 at 18:59 UTC

    sub standard_fill{ return substr $_[ 0 ] . ' ' x 100, 0, $_[ 1 ] }

    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    Lingua non convalesco, consenesco et abolesco.
    Rule 1 has a caveat! -- Who broke the cabal?
Re: Generating standard length strings
by Corion (Patriarch) on Apr 14, 2005 at 19:04 UTC

    As nobody has mentioned it already, there is another way:

    sub rpad { pack "A$_[1]", $_[0]};
      Note that your sub will cut off the input string if its length is larger than the padding width:
      sub rpad { pack "A$_[1]", $_[0] }; my $string = 'averylongstringthatislongerthan10charsforsure!'; print "]", rpad($string, 10), "[\n"; __END__ ]averylongs[
      I am not sure if that is what the OP intended.
Re: Generating standard length strings
by jdporter (Paladin) on Apr 14, 2005 at 18:59 UTC
    sub standard_fill { my( $string, $length ) = @_; sprintf "%-${length}s", $string }
Re: Generating standard length strings
by crashtest (Curate) on Apr 14, 2005 at 19:04 UTC
    And just for variety (TIMTOWTDI, after all), I first thought of using pack:
    sub pad{ my ($string, $length) = @_; return (length($string) < $length ? pack("A$length", $string) : $st +ring); }
    ... although after reading all the responses, I agree that sprintf is the best way to go.
Re: Generating standard length strings
by RazorbladeBidet (Friar) on Apr 14, 2005 at 19:00 UTC
    You want string multiplication a la:
    sub standard_fill { my $string = $_[0]; my $length = $_[1]; my $orig_string_length = length($string); $string .= " " x ($length - $orig_string_length); return $string }
    (code untested)

    I'll leave the golfing to the PGA tour ;)
    --------------
    "But what of all those sweet words you spoke in private?"
    "Oh that's just what we call pillow talk, baby, that's all."
Re: Generating standard length strings
by Fletch (Bishop) on Apr 14, 2005 at 19:00 UTC
    my $str = sprintf( "%${len}s", $str );

    perldoc -f sprintf if you need to do anything fancier like left justify.

    Update: D'oh, the original question was about left justified. Not like there's not 15 bazillion other answers to pick from . . . :)