But I wonder if printf will do it for me.
I'd like to use what I think are called "leaders" ... where the output
would something like this: (monospace text ahead)
left_column_info . . . . . . . . . right_column_info
long_left_column_info . . . . . . right_column_info
Greetings. I'm sure this is probably an FAQ, but I can't find it.
I have a script that basically does this:
printf "%-38s%s\n",left_column_info,right_column_info;
The problem is that when the left_column_info is too short, there's a big gap.
I'd like to use what I think are called "leaders" ... where the output
would something like this: (monospace text ahead)
left_column_info . . . . . . . . . right_column_info
long_left_column_info . . . . . . right_column_info
Is there an easy way to do this, where there is
* at least one space after left_column_info
* the dots are aligned in columns
?
The only thing I can think of is
* get the length(left_column_info)
* add 1 space if the number is (let's say) even
* add " ." until the length is (in this case) 37
But I wonder if printf will do it for me.
printf ("%-*s %s %s\n", $len_left, $left_string, '.'x$len_dots, $right_string);
On 11/22/23 09:22, HASM wrote:
Awesome program that I'm now playing with.
printf ("%-*s %s %s\n", $len_left, $left_string, '.'x$len_dots, $right_string);
Wouldn't that end up with:
left_column_info...................right_column_info long_left_column_info..............right_column_info
Though I do wonder if you might be able to tweak that to be something
akin to:
And I think it does exactly what I want.
printf ("%-*s %s %s\n", $len_left, $left_string, '.'x$len_dots,Wouldn't that end up with:
$right_string);
left_column_info...................right_column_info
long_left_column_info..............right_column_info
Thank you very much HASM
Greetings. I'm sure this is probably an FAQ, but I can't find it.
I have a script that basically does this:
printf "%-38s%s\n",left_column_info,right_column_info;
The problem is that when the left_column_info is too short, there's a big gap.
I'd like to use what I think are called "leaders" ... where the output
would something like this: (monospace text ahead)
left_column_info . . . . . . . . . right_column_info
long_left_column_info . . . . . . right_column_info
The only thing I can think of is
* get the length(left_column_info)
* add 1 space if the number is (let's say) even
* add " ." until the length is (in this case) 37
But I wonder if printf will do it for me.
printf ("%-*s %s %s\n", $len_left, $left_string, '.'x$len_dots,Wouldn't that end up with:
$right_string);
left_column_info...................right_column_info
long_left_column_info..............right_column_info
Not quite, I misread the initial requirement, but did bracket the dot sequence with spaces, i,e., one would get
left column info ...................... right_column_info
long_left column info ................. right_column_info
Thank you very much HASM
You're welcome.
I think I've only used printf's * field width once (and also $
positional ordering) more than 20 years ago on a C program, but your
post triggered my memory and I was almost certain Perl would have it.
hymie! <hymie@nasalinux.net> writes:
The only thing I can think of is
* get the length(left_column_info)
* add 1 space if the number is (let's say) even
* add " ." until the length is (in this case) 37
But I wonder if printf will do it for me.
Why not just translate your algorithm into Perl?
-----
sub dot_print {
my ($w, $l, $r) = @_;
$l .= ' ' unless length($l) & 1;
$l .= ' .' while length($l) < $w;
print($l, ' ', $r, "\n");
}
dot_print(38, 'left_column_info', 'right_column_info');
dot_print(38, 'long_left_column_info', 'right_column_info');
-----
Sysop: | Keyop |
---|---|
Location: | Huddersfield, West Yorkshire, UK |
Users: | 497 |
Nodes: | 16 (2 / 14) |
Uptime: | 68:39:04 |
Calls: | 9,766 |
Calls today: | 7 |
Files: | 13,747 |
Messages: | 6,186,073 |