• printf with trailing dots ?

    From hymie!@21:1/5 to All on Wed Nov 22 13:30:03 2023
    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.

    Thanks.

    --hymie! http://nasalinux.net/~hymie hymie@nasalinux.net

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Grant Taylor@21:1/5 to All on Wed Nov 22 08:32:20 2023
    On 11/22/23 07:30, hymie! wrote:
    But I wonder if printf will do it for me.

    It's been too long since I've done anything like this so I don't know.

    I do think that printf had an option where you could specify the
    character that was used to pad with. I don't know if that had to be a
    singular character or if it could e a multi-character pattern. --
    though I don't know what the alignment would be like.

    One trick I would wonder about if you were printing to a terminal would
    be to print the entire line with the dot pattern space and then over
    print it with the left_column and a space. But that might not work for
    your application.



    Grant. . . .

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From HASM@21:1/5 to hymie@nasalinux.net on Wed Nov 22 07:22:06 2023
    hymie! <hymie@nasalinux.net> writes:

    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 code below ought to do it.
    Maybe add some error checking for the proper arguments to print subroutine.

    -- HASM


    #! /usr/bin/perl

    use Modern::Perl;
    use List::Util qw(max);

    sub dot_printf {
    my $width = shift;
    my $left_string = shift;
    my $right_string = shift;

    my $len_left = length($left_string);
    my $len_dots = max(0, $width-$len_left);
    printf ("%-*s %s %s\n", $len_left, $left_string, '.'x$len_dots, $right_string);
    }

    my $widht = 38;
    dot_printf $widht, 'left string', 'right_string';
    dot_printf $widht, 'larger string', 'right_string';

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Keith Thompson@21:1/5 to hymie@nasalinux.net on Wed Nov 22 08:50:06 2023
    hymie! <hymie@nasalinux.net> writes:
    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.

    I don't think printf will do this directly. You can certainly do it
    with string manipulation, but I think that `format` might be the best
    solution. `perldoc perlform` for details. (Disclaimer: I haven't
    thought about Perl's format feature in years, so I'm not at all sure
    that it's suitable.)

    Other than that, initializing a string with ". . . . . ."... and then overwriting parts of it may be the best solution.

    --
    Keith Thompson (The_Other_Keith) Keith.S.Thompson+u@gmail.com
    Will write code for food.
    void Void(void) { Void(); } /* The recursive call of the void */

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Grant Taylor@21:1/5 to HASM on Wed Nov 22 10:41:35 2023
    On 11/22/23 09:22, HASM wrote:
    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

    Or am I misunderstanding the '.'x$len_dots part?

    Though I do wonder if you might be able to tweak that to be something
    akin to:

    '. 'x$len_dots/2

    Type thing.



    Grant. . . .

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From hymie!@21:1/5 to who on Wed Nov 22 17:41:36 2023
    In our last episode, the evil Dr. Lacto had captured our hero,
    Grant Taylor <gtaylor@tnetconsulting.net>, who said:
    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

    Yes it does.

    Though I do wonder if you might be able to tweak that to be something
    akin to:

    I've got this:

    use List::Util qw(max);

    sub dot_printf {
    my $width = shift;
    my $left_string = shift;
    my $right_string = shift;

    my $len_left = length($left_string);
    $len_left++ if ($len_left % 2);
    my $len_dots = max(0, $width-$len_left);
    printf ("%-*s %s%s\n", $len_left, $left_string, '. 'x ($len_dots/2),
    $right_string);
    }

    And I think it does exactly what I want.

    Thank you very much HASM

    --hymie! http://nasalinux.net/~hymie hymie@nasalinux.net

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Grant Taylor@21:1/5 to All on Wed Nov 22 13:17:27 2023
    On 11/22/23 11:41, hymie! wrote:
    And I think it does exactly what I want.

    Cool.

    My concern would be ending up with lines of output not having dots line
    up, depending on the length of the left column. Even vs odd length type
    thing.

    If that does happen, you could probably add a space for the remainder
    (modulus) of the length of the left column divided by two.



    Grant. . . .

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From HASM@21:1/5 to All on Thu Nov 23 07:38:36 2023
    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

    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.

    -- HASM

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Rainer Weikusat@21:1/5 to hymie@nasalinux.net on Thu Nov 23 19:35:23 2023
    hymie! <hymie@nasalinux.net> writes:
    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.

    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');
    -----

    IMHO, doing anything more fancy just makes the code needlessly
    complicated.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Ben Bacarisse@21:1/5 to HASM on Thu Nov 23 19:21:50 2023
    HASM <hasm@example.invalid> writes:

    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

    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.

    But why? printf("%-*s", length($s), $s) is just printf("%s", $s).

    --
    Ben.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Rainer Weikusat@21:1/5 to Rainer Weikusat on Thu Nov 23 21:52:57 2023
    Rainer Weikusat <rweikusat@talktalk.net> writes:
    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');
    -----

    Slight correction: The lines produced by the code above are too long.

    ----
    sub dot_print {
    my ($w, $l, $r) = @_;

    $l .= ' ' unless length($l) & 1;
    $l .= ' .' while $w - length($l) > 1;
    print($l, ' ', $r, "\n");
    }

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)