* namerefs to var[idx]
@ 2024-03-08 13:23 Stephane Chazelas
2024-03-08 18:30 ` Bart Schaefer
0 siblings, 1 reply; 2+ messages in thread
From: Stephane Chazelas @ 2024-03-08 13:23 UTC (permalink / raw)
To: Zsh hackers list
$ f() { typeset -nu v=$1; echo ${v[1]}; }
$ a=abcd
$ f 'a[2,3]'
b
OK but:
$ f() { typeset -nu v=$1; echo $v[1]; }
$ f 'a[2,3]'
f: no matches found: bc[1]
It's amazing that these actually work:
$ f() { typeset -nu v=$1; v[1]=foo; }
$ f a
$ echo $a
foo2345
$ f 'a[6]'
$ echo $a
foo23foo5
This one not really:
$ a=12345
$ f 'a[2,3]'
$ echo $a
1foo45
In that one could expect the first character of the "23"
substring to be changed to "foo", but I can see that's asking
quite a lot.
--
Stephane
^ permalink raw reply [flat|nested] 2+ messages in thread
* Re: namerefs to var[idx]
2024-03-08 13:23 namerefs to var[idx] Stephane Chazelas
@ 2024-03-08 18:30 ` Bart Schaefer
0 siblings, 0 replies; 2+ messages in thread
From: Bart Schaefer @ 2024-03-08 18:30 UTC (permalink / raw)
To: Zsh hackers list
On Fri, Mar 8, 2024 at 5:23 AM Stephane Chazelas <stephane@chazelas.org> wrote:
>
> $ f() { typeset -nu v=$1; echo $v[1]; }
> $ f 'a[2,3]'
> f: no matches found: bc[1]
Things are pretty tangled here: paramsubst() calls fetchvalue(). In
${v[1]} the entire reference is handled by fetchvalue(). In $v[1],
the $v is handled by fetchvalue() and then the [1] is handled by
paramsubst(). So it ends up being treated as $a[2,3][1], which
produces the same error. I can't immediately think of any way to fix
that without breaking something else ... ideally we'd make $a[2,3][1]
work instead, but that's a pretty large change.
In the assignment case ...
> $ f() { typeset -nu v=$1; v[1]=foo; }
... the left side of the assignment is interpreted more like ${v[1]}
than like $v[1], so everything "works." I guess one must remember
that omitting the braces around subscript expansions has always been a
shortcut rather than "the right way to do it". The braces are
required for correct handling of namespaces, too.
> $ a=12345
> $ f 'a[2,3]'
> $ echo $a
> 1foo45
>
> In that one could expect the first character of the "23"
> substring to be changed
I say "more like" above because:
% a[2,3][1]=b
zsh: no matches found: a[2,3][1]=b
% typeset 'a[2,3][1]'=c
zsh: not an identifier: a[2,3][1]
So using a nameref that way is rather a back door around the check for
identifiers.
This is an interesting variation:
% f() { typeset -n x='v[2]'; typeset -nu v=$1; x=foo; typeset -p v x }
% f 'a[2,3]'
typeset -un v='afoo2,3]'
typeset -n x='v[2]'
So chaining subscripted namerefs isn't really working, probably
because of something similar to the paramsubst()/fetchvalue() division
of labor just mentioned.
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2024-03-08 18:30 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-03-08 13:23 namerefs to var[idx] Stephane Chazelas
2024-03-08 18:30 ` Bart Schaefer
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).