Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: create variable with calculated number of spaces.



or, more efficiently:

    printf -v spacer "%${diff}s" ''

On Mon, May 6, 2024 at 6:35 PM Mark J. Reed <markjreed@xxxxxxxxx> wrote:
Well, mixed up your var names. This is what I meant:

    spacer=$(printf "%${diff}s" '')


On Mon, May 6, 2024 at 6:33 PM Mark J. Reed <markjreed@xxxxxxxxx> wrote:
printf "%${spacer}s" "" ?

Mark J. Reed <markjreed@xxxxxxxxx>


On Mon, May 6, 2024 at 18:04 Ray Andrews <rayandrews@xxxxxxxxxxx> wrote:
So I want to adjust the length of string1. I do this  by inserting a spacer in string1 who's length is calculated from the length of string2:

    local diff spacer
    (( diff = $#string2 - 76 ))
    for (( dd=1; dd<diff; dd++ )); do spacer+=' '; done
    ... # spacer will be incorporated into string1, not shown.

It works fine, but I'll bet there's a less laborious way -- some way of creating 'spacer' with  'diff' number of spaces without the for loop.  I could make 'spacer' too long and then cut it at an index [diff], but I doubt that's as simple as it could be.  I know it's there, I just can't quite remember. 




--
Mark J. Reed <markjreed@xxxxxxxxx>


--
Mark J. Reed <markjreed@xxxxxxxxx>


Messages sorted by: Reverse Date, Date, Thread, Author