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)
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.