Now, before you set this on up to get reaped, I wasnt sure where golf-type stuff should go. /me wonders to this day. Anyway:

While coding away, I realized I needed to make strings fit pre-determined column lengths. So I decided to whip up a subroutine to take in the length, and a ref to the string.

I thought I had seen a discussion in a book somewhere about using ###'s to help format the columns in a flat file, but couldnt find it again. *sigh*

So I came up with this:
print FILE format_text(length => 10,string => \$string); ######################## ## format_text ######################## sub format_text { my %args = (@_); ${$args{string}} = substr(${$args{string}},0, $args{length}); return "${$args{string}}" . ' 'x($args{length} -length(${$args +{string}})); }
So there we have it. Not to shabby, if I dont say so myself. I'm still learning, and enjoying every day of it, so the way I figure it, yay. Alas, I am curious as to weither or not there is a glarningly obvious way to do this that I've overlooked.

So, can I do it quicker? In shorter code? Any thoughts?

_14k4 - webmaster@poorheart.com (www.poorheart.com)

Replies are listed 'Best First'.
Re: (golf)String Concats or: playing with substr()
by Masem (Monsignor) on May 18, 2001 at 23:13 UTC
    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
      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;
Re: (golf)String Concats or: playing with substr()
by ChemBoy (Priest) on May 19, 2001 at 00:35 UTC

    I wanted there to be a solution using write, but it won't pad out trailing whitespace for you. It could be done using right-justification, but I assume that's not what you want to do. On the other hand, if you're looking at multiple columns, it has merits (you can get around the padding problem, if you want to use read, but then you might as well use printf).

    format FILE = @<<<<<<<<<@<<<<<<<<@<<<<<<<<< @foo . for my $line (@LoL) { @foo = @$line; write FILE; }

    (The hacks to force a constant record length that I've come up with while writing this are either to right-justify the last field or put in a dummy field at the end with a useless character in it, btw--I'm sure there are others.)



    If God had meant us to fly, he would *never* have give us the railroads.
        --Michael Flanders

      See, this is was was in on the tip of my tongue, but I couldnt think of the syntax, nor even was it was called. Sigh. I'll give that a shot though. TMTOWTDI, of course, and maybe I should benchmark the different ways. Heck, I'd learn benchmark. ;)

      _14k4 - webmaster@poorheart.com (www.poorheart.com)
Re: (golf)String Concats or: playing with substr()
by srawls (Friar) on May 19, 2001 at 00:45 UTC
    First of all, this solution was just for fun, I don't recomend using it; that said:

    I wanted a solution that used one regex, so here it is:

    $text =~s/(.{1,$length}).*/ length($1)<$length?$"x($length-length($1)).$1:$1/e;


    The 15 year old, freshman programmer,
    Stephen Rawls