* subscript not working as "I" expected it would
@ 2023-11-04 7:08 Jim
[not found] ` <CAN=4vMowpVVYPfkeTnwXEbUUU48UNMwPGnjGi3wWYFVkx21YdA@mail.gmail.com>
0 siblings, 1 reply; 3+ messages in thread
From: Jim @ 2023-11-04 7:08 UTC (permalink / raw)
To: zsh
[-- Attachment #1: Type: text/plain, Size: 906 bytes --]
Hi everyone,
Case: Outputting a hash from a git repository, but want it abbreviated.
This is done totally via z-shell script, without git commands.
Taking a bash/sed example and converting it to zsh.
Sorry if that is to much information.
With
print -- $(<.git/${(s.: .)$(<.git/HEAD)[2]})
I get the full 40 character hash as expected
but when I tried to output the first 9 characters
print -- ${$(<.git/${(s.: .)$(<.git/HEAD)[2]})[1,9]}
it too, returns the full 40 character hash and no warnings or error
messages.
After beating my head for a while, it dawned on me to try quoting
print -- ${"$(<.git/${(s.: .)$(<.git/HEAD)[2]})"[1,9]}
which worked, returning the first 9 characters of the hash.
Could someone explain why the quotes are needed here so hopefully
the next time I will understand. I swear zsh quoting will drive me nuts.
Thanks for putting up with me.
Regards,
Jim Murphy
[-- Attachment #2: Type: text/html, Size: 1270 bytes --]
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: subscript not working as "I" expected it would
[not found] ` <CAN=4vMowpVVYPfkeTnwXEbUUU48UNMwPGnjGi3wWYFVkx21YdA@mail.gmail.com>
@ 2023-11-04 8:48 ` Roman Perepelitsa
2023-11-04 16:37 ` Lawrence Velázquez
0 siblings, 1 reply; 3+ messages in thread
From: Roman Perepelitsa @ 2023-11-04 8:48 UTC (permalink / raw)
To: linuxtechguy; +Cc: Zsh Users
[cc:zsh-users]
On Sat, Nov 4, 2023 at 9:47 AM Roman Perepelitsa
<roman.perepelitsa@gmail.com> wrote:
>
> On Sat, Nov 4, 2023 at 8:09 AM Jim <linux.tech.guy@gmail.com> wrote:
> >
> > After beating my head for a while, it dawned on me to try quoting
> > print -- ${"$(<.git/${(s.: .)$(<.git/HEAD)[2]})"[1,9]}
> > which worked, returning the first 9 characters of the hash.
> >
> > Could someone explain why the quotes are needed here so hopefully
> > the next time I will understand. I swear zsh quoting will drive me nuts.
>
> $(list) expands to an array. ${$(list)[N]} gives the Nth element of the array.
>
> % print -r -- ${$(print foo bar)[1]}
> foo
> % print -r -- ${$(print foo bar)[2]}
> bar
>
> $(list) can be an array with just one element but it's still an array.
> This is the case in your code snippet.
>
> "$(list)" on the other hand is a scalar. ${"$(list)"[N]} gives the Nth
> character.
>
> % print -r -- ${"$(print foo bar)"[1]}
> f
> % print -r -- ${"$(print foo bar)"[2]}
> o
>
> Roman.
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: subscript not working as "I" expected it would
2023-11-04 8:48 ` Roman Perepelitsa
@ 2023-11-04 16:37 ` Lawrence Velázquez
0 siblings, 0 replies; 3+ messages in thread
From: Lawrence Velázquez @ 2023-11-04 16:37 UTC (permalink / raw)
To: linuxtechguy; +Cc: zsh-users
On Sat, Nov 4, 2023, at 4:48 AM, Roman Perepelitsa wrote:
>> $(list) expands to an array. ${$(list)[N]} gives the Nth element of the array.
>>
>> % print -r -- ${$(print foo bar)[1]}
>> foo
>> % print -r -- ${$(print foo bar)[2]}
>> bar
>>
>> $(list) can be an array with just one element but it's still an array.
>> This is the case in your code snippet.
>>
>> "$(list)" on the other hand is a scalar. ${"$(list)"[N]} gives the Nth
>> character.
>>
>> % print -r -- ${"$(print foo bar)"[1]}
>> f
>> % print -r -- ${"$(print foo bar)"[2]}
>> o
Also note that subscripting occurs *before* (s) is applied, so your
(s.: .) is not doing what you think it is doing.
% printf '<%s>' ${(s.: .)$(echo a b: c d)[2]}; echo
<b:>
% printf '<%s>' ${${(s.: .)"$(echo a b: c d)"}[2]}; echo
<c d>
--
vq
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2023-11-04 16:40 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-11-04 7:08 subscript not working as "I" expected it would Jim
[not found] ` <CAN=4vMowpVVYPfkeTnwXEbUUU48UNMwPGnjGi3wWYFVkx21YdA@mail.gmail.com>
2023-11-04 8:48 ` Roman Perepelitsa
2023-11-04 16:37 ` Lawrence Velázquez
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).