in reply to (golf)String Concats or: playing with substr()

Sure, as sprintf makes this nice and easy.
sprintf( "%${length}s", $string );

UpdateYeah, yeah, I meant "s" in the format string instead of "f". Really, I did! (I had to look up what the string code was on sprintf's page!)


Dr. Michael K. Neylon - mneylon-pm@masemware.com || "You've left the lens cap of your mind on again, Pinky" - The Brain

Replies are listed 'Best First'.
Re: Re: (golf)String Concats or: playing with substr()
by one4k4 (Hermit) on May 18, 2001 at 23:31 UTC
    It looks as if this way will take all my strings and turn them into floats if possible. Making them $args{length} long. Which is just weird. I mean, I couldnt get it to work. But then again, you're masem, and I'm one4k4. ;)

    sub format_text { my %args = (@_); sprintf( "%$args{length}f", $args{string} ); }
    I would post results, but the data is sensitive. (SS#'s, names, and such...)

    Hrm, back to said drawing board I go. I was thinking sprintf may help, but wasnt 100% sure. I'm playing around for the rest of the afternoon anyway, so I might as well learn somethin'.

    _14k4 - webmaster@poorheart.com (www.poorheart.com)
      I suspect that Masem meant to use "%s" rather than "%f": sprintf "%${length}s", $string;
        Or rather:

        sprintf "%${length}.${length}s", $string;

        as it is the number following the dot that gives the maximum field width when using the s. You could also left-justify by throwing a - in after the % if you like.

        --
        I'd like to be able to assign to an luser