zsh-users
 help / color / mirror / Atom feed
* nested parameter expansion
@ 2020-09-24  8:03 jdh
  2020-09-24  8:15 ` Peter Stephenson
  0 siblings, 1 reply; 4+ messages in thread
From: jdh @ 2020-09-24  8:03 UTC (permalink / raw)
  To: zsh-users


I can't get, what should be simple nested parameter to work.
I think that $ndx needs to be expanded before the letter P before it 1st.

Simple example:

    ndx=1
    P1_ARRAY=("1" "2" "3")
    
    print ${P${ndx}_ARRAY}
#results in: "zsh bad substitution

    print ${~P${ndx}_ARRAY}
#results in: "zsh bad substitution

    print ${(P)P${ndx}_ARRAY}
#results in: "zsh bad substitution

I want to do a simple 'for' loop like this:

  for ndx in ("1" "2" "3")		# for multiple arrays not defined in this simple example.
      do
      for element in ${P${ndx}_ARRAY}
        do
	print "Process element."
	done
     done


Any assistance would be appreciated





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

* Re: nested parameter expansion
  2020-09-24  8:03 nested parameter expansion jdh
@ 2020-09-24  8:15 ` Peter Stephenson
  2020-09-24  8:29   ` Roman Perepelitsa
  2020-09-24 17:07   ` jdh
  0 siblings, 2 replies; 4+ messages in thread
From: Peter Stephenson @ 2020-09-24  8:15 UTC (permalink / raw)
  To: zsh-users


> On 24 September 2020 at 09:03 jdh <dhenman@gmail.com> wrote:
> I can't get, what should be simple nested parameter to work.
> I think that $ndx needs to be expanded before the letter P before it 1st.
> 
> Simple example:
> 
>     ndx=1
>     P1_ARRAY=("1" "2" "3")
>     
>     print ${P${ndx}_ARRAY}
> #results in: "zsh bad substitution

I don't think there's a simple way to do it without an extra intermediate
variable:

var="P${ndx}_ARRAY"
print ${(P)var}

You could add some eval trickery, but I suspect it's not worth making it any
more complicated.

pws


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

* Re: nested parameter expansion
  2020-09-24  8:15 ` Peter Stephenson
@ 2020-09-24  8:29   ` Roman Perepelitsa
  2020-09-24 17:07   ` jdh
  1 sibling, 0 replies; 4+ messages in thread
From: Roman Perepelitsa @ 2020-09-24  8:29 UTC (permalink / raw)
  To: Zsh Users

On Thu, Sep 24, 2020 at 10:15 AM Peter Stephenson
<p.w.stephenson@ntlworld.com> wrote:
>
> You could add some eval trickery, but I suspect it's not worth making it any
> more complicated.

This should work:

    print ${(P)${:-P${ndx}_ARRAY}}

I wouldn't recommend writing it this way. It's quite a bit more
cryptic than using an intermediate variable.

Roman.


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

* Re: nested parameter expansion
  2020-09-24  8:15 ` Peter Stephenson
  2020-09-24  8:29   ` Roman Perepelitsa
@ 2020-09-24 17:07   ` jdh
  1 sibling, 0 replies; 4+ messages in thread
From: jdh @ 2020-09-24 17:07 UTC (permalink / raw)
  To: zsh-users


Thanks for the information Roman & Peter.
Much appreciated.

---------

Peter Stephenson <p.w.stephenson@ntlworld.com> wrote:
> 
> > On 24 September 2020 at 09:03 jdh <dhenman@gmail.com> wrote:
> > I can't get, what should be simple nested parameter to work.
> > I think that $ndx needs to be expanded before the letter P before it 1st.
> > 
> > Simple example:
> > 
> >     ndx=1
> >     P1_ARRAY=("1" "2" "3")
> >     
> >     print ${P${ndx}_ARRAY}
> > #results in: "zsh bad substitution
> 
> I don't think there's a simple way to do it without an extra intermediate
> variable:
> 
> var="P${ndx}_ARRAY"
> print ${(P)var}
> 
> You could add some eval trickery, but I suspect it's not worth making it any
> more complicated.
> 
> pws
> 



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

end of thread, other threads:[~2020-09-24 17:08 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-09-24  8:03 nested parameter expansion jdh
2020-09-24  8:15 ` Peter Stephenson
2020-09-24  8:29   ` Roman Perepelitsa
2020-09-24 17:07   ` jdh

zsh-users

This inbox may be cloned and mirrored by anyone:

	git clone --mirror http://inbox.vuxu.org/zsh-users

	# If you have public-inbox 1.1+ installed, you may
	# initialize and index your mirror using the following commands:
	public-inbox-init -V1 zsh-users zsh-users/ http://inbox.vuxu.org/zsh-users \
		zsh-users@zsh.org
	public-inbox-index zsh-users

Example config snippet for mirrors.
Newsgroup available over NNTP:
	nntp://inbox.vuxu.org/vuxu.archive.zsh.users


code repositories for the project(s) associated with this inbox:

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

AGPL code for this site: git clone https://public-inbox.org/public-inbox.git