zsh-users
 help / color / mirror / code / Atom feed
* create variable with calculated number of spaces.
@ 2024-05-06 22:03 Ray Andrews
  2024-05-06 22:33 ` Mark J. Reed
  0 siblings, 1 reply; 11+ messages in thread
From: Ray Andrews @ 2024-05-06 22:03 UTC (permalink / raw)
  To: Zsh Users

[-- Attachment #1: Type: text/plain, Size: 647 bytes --]

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.


[-- Attachment #2: Type: text/html, Size: 958 bytes --]

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: create variable with calculated number of spaces.
  2024-05-06 22:03 create variable with calculated number of spaces Ray Andrews
@ 2024-05-06 22:33 ` Mark J. Reed
  2024-05-06 22:35   ` Mark J. Reed
  0 siblings, 1 reply; 11+ messages in thread
From: Mark J. Reed @ 2024-05-06 22:33 UTC (permalink / raw)
  To: Ray Andrews; +Cc: Zsh Users

[-- Attachment #1: Type: text/plain, Size: 789 bytes --]

printf "%${spacer}s" "" ?

Mark J. Reed <markjreed@gmail.com>


On Mon, May 6, 2024 at 18:04 Ray Andrews <rayandrews@eastlink.ca> 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.
>
>
>

[-- Attachment #2: Type: text/html, Size: 1692 bytes --]

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: create variable with calculated number of spaces.
  2024-05-06 22:33 ` Mark J. Reed
@ 2024-05-06 22:35   ` Mark J. Reed
  2024-05-06 22:38     ` Mark J. Reed
  0 siblings, 1 reply; 11+ messages in thread
From: Mark J. Reed @ 2024-05-06 22:35 UTC (permalink / raw)
  To: Zsh Users

[-- Attachment #1: Type: text/plain, Size: 1056 bytes --]

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@gmail.com> wrote:

> printf "%${spacer}s" "" ?
>
> Mark J. Reed <markjreed@gmail.com>
>
>
> On Mon, May 6, 2024 at 18:04 Ray Andrews <rayandrews@eastlink.ca> 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@gmail.com>

[-- Attachment #2: Type: text/html, Size: 2396 bytes --]

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: create variable with calculated number of spaces.
  2024-05-06 22:35   ` Mark J. Reed
@ 2024-05-06 22:38     ` Mark J. Reed
  2024-05-06 23:02       ` Ray Andrews
  2024-05-06 23:57       ` Lawrence Velázquez
  0 siblings, 2 replies; 11+ messages in thread
From: Mark J. Reed @ 2024-05-06 22:38 UTC (permalink / raw)
  To: Zsh Users

[-- Attachment #1: Type: text/plain, Size: 1283 bytes --]

or, more efficiently:

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

On Mon, May 6, 2024 at 6:35 PM Mark J. Reed <markjreed@gmail.com> 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@gmail.com> wrote:
>
>> printf "%${spacer}s" "" ?
>>
>> Mark J. Reed <markjreed@gmail.com>
>>
>>
>> On Mon, May 6, 2024 at 18:04 Ray Andrews <rayandrews@eastlink.ca> 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@gmail.com>
>


-- 
Mark J. Reed <markjreed@gmail.com>

[-- Attachment #2: Type: text/html, Size: 3129 bytes --]

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: create variable with calculated number of spaces.
  2024-05-06 22:38     ` Mark J. Reed
@ 2024-05-06 23:02       ` Ray Andrews
  2024-05-06 23:28         ` Mark J. Reed
  2024-05-06 23:57       ` Lawrence Velázquez
  1 sibling, 1 reply; 11+ messages in thread
From: Ray Andrews @ 2024-05-06 23:02 UTC (permalink / raw)
  To: zsh-users



On 2024-05-06 15:38, Mark J. Reed wrote:
> or, more efficiently:
>
>     printf -v spacer "%${diff}s" ''
Nice and compact.  One thing tho, I'm not sure this is the correct way 
to think, but I have an instinct to stay away from 'fancy' commands like 
printf and to rely on native zsh code where possible.  Legit?  Or false 
frugality?  What I half-remember is some loop that has an iteration 
counter.  Mind, there was one of Roman's speed tests where he used an 
external command and had a huge performance improvement, so thereyago.



^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: create variable with calculated number of spaces.
  2024-05-06 23:02       ` Ray Andrews
@ 2024-05-06 23:28         ` Mark J. Reed
  2024-05-06 23:41           ` Ray Andrews
  0 siblings, 1 reply; 11+ messages in thread
From: Mark J. Reed @ 2024-05-06 23:28 UTC (permalink / raw)
  Cc: zsh-users

[-- Attachment #1: Type: text/plain, Size: 929 bytes --]

The printf command is built into zsh. I'm not sure what makes it "fancy" or
non-native.

(Of the three printing commands it's also the one that works the same
across bash, ksh, and zsh, but that's not a concern if you're writing a
specifically zsh script.)

Mark J. Reed <markjreed@gmail.com>


On Mon, May 6, 2024 at 19:04 Ray Andrews <rayandrews@eastlink.ca> wrote:

>
>
> On 2024-05-06 15:38, Mark J. Reed wrote:
> > or, more efficiently:
> >
> >     printf -v spacer "%${diff}s" ''
> Nice and compact.  One thing tho, I'm not sure this is the correct way
> to think, but I have an instinct to stay away from 'fancy' commands like
> printf and to rely on native zsh code where possible.  Legit?  Or false
> frugality?  What I half-remember is some loop that has an iteration
> counter.  Mind, there was one of Roman's speed tests where he used an
> external command and had a huge performance improvement, so thereyago.
>
>
>

[-- Attachment #2: Type: text/html, Size: 1650 bytes --]

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: create variable with calculated number of spaces.
  2024-05-06 23:28         ` Mark J. Reed
@ 2024-05-06 23:41           ` Ray Andrews
  0 siblings, 0 replies; 11+ messages in thread
From: Ray Andrews @ 2024-05-06 23:41 UTC (permalink / raw)
  To: zsh-users



On 2024-05-06 16:28, Mark J. Reed wrote:
> The printf command is built into zsh. I'm not sure what makes it 
> "fancy" or non-native.
Well then that's that.  I think of printf as /usr/bin/printf, but if zsh 
has one internal, no further questions.

Thanks Mark.



^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: create variable with calculated number of spaces.
  2024-05-06 22:38     ` Mark J. Reed
  2024-05-06 23:02       ` Ray Andrews
@ 2024-05-06 23:57       ` Lawrence Velázquez
  2024-05-07  5:14         ` Ray Andrews
  1 sibling, 1 reply; 11+ messages in thread
From: Lawrence Velázquez @ 2024-05-06 23:57 UTC (permalink / raw)
  To: zsh-users

On Mon, May 6, 2024, at 6:38 PM, Mark J. Reed wrote:
> or, more efficiently:
>
>     printf -v spacer "%${diff}s" ''

And if the spacing is just being added to the beginning or end of
string1, then don't bother with the spacer variable:

	# Left-padded
	printf -v string1 '%*s' $#string2 $string1

	# Right-padded
	printf -v string1 '%-*s' $#string2 $string1

-- 
vq


^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: create variable with calculated number of spaces.
  2024-05-06 23:57       ` Lawrence Velázquez
@ 2024-05-07  5:14         ` Ray Andrews
  2024-05-07  5:52           ` Roman Perepelitsa
  0 siblings, 1 reply; 11+ messages in thread
From: Ray Andrews @ 2024-05-07  5:14 UTC (permalink / raw)
  To: zsh-users

[-- Attachment #1: Type: text/plain, Size: 2125 bytes --]



On 2024-05-06 16:57, Lawrence Velázquez wrote:
> And if the spacing is just being added to the beginning or end of
> string1, then don't bother with the spacer variable:
>
> 	# Left-padded
> 	printf -v string1 '%*s' $#string2 $string1
>
> 	# Right-padded
> 	printf -v string1 '%-*s' $#string2 $string1
Gotta start using printf to cobble together complicated variables. But 
in this case it's internal:

% mnt

Label:     Partition:    Mountpoint:  Size:    Free:    Used: %Used:

b6-12-Deb12b     sda6    /            11.7G     5.6G     5.5G 47%
b3--0-aWorking   sda3    /aWorking   280.5M   157.2M   104.3M 37%
b2--0-boot       sda2    /boot       188.2M   131.2M    43.0M 23%
b8--6-root1      sda8    /root         5.8G     2.4G     3.1G 53%
b11---aRay       sda11   /aRay        15.6G     6.2G     8.5G 55%
b10-5-aMisc      sda10   /aMisc        4.8G     4.5G   121.1M 2%

% mnt sda

Label:     Partition:    Mountpoint:     Size:    Free:    Used: %Used:

b6-12-Deb12b     sda6    /               11.7G     5.6G 5.5G    47%
b3--0-aWorking   sda3    /aWorking      280.5M   157.2M 104.3M    37%
b2--0-boot       sda2    /boot          188.2M   131.2M 43.0M    23%
b8--6-root1      sda8    /root            5.8G     2.4G 3.1G    53%
b11---aRay       sda11   /aRay           15.6G     6.2G 8.5G    55%
b10-5-aMisc      sda10   /aMisc           4.8G     4.5G 121.1M     2%
b5-12-Deb11a     sda5    /media/sda/5    11.7G     4.2G 6.8G    58%
b7-12-Deb12c     sda7    /media/sda/7    11.7G     5.6G 5.5G    47%
b9--6-root2      sda9    /media/sda/9     5.8G     2.4G 3.1G    53%

... 'findmnt' outputs nice columns so the thing is to space the 'title' 
line accordingly -- it's mostly the mountpoint who's width can vary so 
the spacer goes in there.  Purely cosmetic.





[-- Attachment #2: Type: text/html, Size: 3275 bytes --]

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: create variable with calculated number of spaces.
  2024-05-07  5:14         ` Ray Andrews
@ 2024-05-07  5:52           ` Roman Perepelitsa
  2024-05-07 14:56             ` Ray Andrews
  0 siblings, 1 reply; 11+ messages in thread
From: Roman Perepelitsa @ 2024-05-07  5:52 UTC (permalink / raw)
  To: Ray Andrews; +Cc: zsh-users

On Tue, May 7, 2024 at 7:15 AM Ray Andrews <rayandrews@eastlink.ca> wrote:
>
> ... 'findmnt' outputs nice columns so the thing is to space the
> 'title' line accordingly -- it's mostly the mountpoint who's width
> can vary so the spacer goes in there.  Purely cosmetic.

You don't need to "cobble together complicated variables" here. Just
printf straight away to stdout.

On the occasion when you do need to justify/pad a parameter without
printing, you can use `printf -v` or this:

    % n=10
    % y=${(r:n:: :)}  # n spaces
    % x='hello'
    % z=${(r:n:: :)x}  # right-justified x

There is also l (lower-case L) for left-justification.

Roman


^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: create variable with calculated number of spaces.
  2024-05-07  5:52           ` Roman Perepelitsa
@ 2024-05-07 14:56             ` Ray Andrews
  0 siblings, 0 replies; 11+ messages in thread
From: Ray Andrews @ 2024-05-07 14:56 UTC (permalink / raw)
  To: zsh-users

[-- Attachment #1: Type: text/plain, Size: 874 bytes --]



On 2024-05-06 22:52, Roman Perepelitsa wrote:
> You don't need to "cobble together complicated variables" here. Just
> printf straight away to stdout.
I know, I  mean in other situations where the variable really is 
complicated.  I've seen 'printf  -v' offered previously to do some 
pretty fancy stuff.

>      % n=10
>      % y=${(r:n:: :)}  # n spaces
>      % x='hello'
>      % z=${(r:n:: :)x}  # right-justified x
Jim Murphy offered:

         spacer=${(l:diff:::: :)}

... very much in the same school of thought.  Feels more 'direct', if 
that means anything.  Mark suggests I shouldn't worry so much about 
'economy' -- I tend to not want to use an elephant's worth of 
functionality ( printf ) where a mouses worth will do.  But maybe that's 
not a valid way to think cuz I can't really anticipate how the shell 
will actually get the work done.




[-- Attachment #2: Type: text/html, Size: 1580 bytes --]

^ permalink raw reply	[flat|nested] 11+ messages in thread

end of thread, other threads:[~2024-05-07 14:57 UTC | newest]

Thread overview: 11+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-05-06 22:03 create variable with calculated number of spaces Ray Andrews
2024-05-06 22:33 ` Mark J. Reed
2024-05-06 22:35   ` Mark J. Reed
2024-05-06 22:38     ` Mark J. Reed
2024-05-06 23:02       ` Ray Andrews
2024-05-06 23:28         ` Mark J. Reed
2024-05-06 23:41           ` Ray Andrews
2024-05-06 23:57       ` Lawrence Velázquez
2024-05-07  5:14         ` Ray Andrews
2024-05-07  5:52           ` Roman Perepelitsa
2024-05-07 14:56             ` Ray Andrews

Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).