zsh-users
 help / color / mirror / code / Atom feed
* Expand each array element with ${(P)...}
@ 2024-02-06  6:50 Marlon Richert
  2024-02-06  7:25 ` Roman Perepelitsa
  0 siblings, 1 reply; 3+ messages in thread
From: Marlon Richert @ 2024-02-06  6:50 UTC (permalink / raw)
  To: Zsh Users

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

I have an array of variable names that I would like to expand to an array
of these variables' values.

I was expecting that if $names is an array, then ${(P)names} – or perhaps
something like ${(PA)names[@]} – would perform ${(P)...} expansion on each
element, but it doesn't. It just expands the first element in the array
instead and leaves out the rest.

Is there any way to make this work without using a for loop? I could not
find any combination of (nested) expansion flags that does what I want.

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

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

* Re: Expand each array element with ${(P)...}
  2024-02-06  6:50 Expand each array element with ${(P)...} Marlon Richert
@ 2024-02-06  7:25 ` Roman Perepelitsa
  2024-02-06  7:40   ` Lawrence Velázquez
  0 siblings, 1 reply; 3+ messages in thread
From: Roman Perepelitsa @ 2024-02-06  7:25 UTC (permalink / raw)
  To: Marlon Richert; +Cc: Zsh Users

On Tue, Feb 6, 2024 at 7:51 AM Marlon Richert <marlon.richert@gmail.com> wrote:
>
> I have an array of variable names that I would like to expand to an
> array of these variables' values.

You could use this (requires extended_glob):

  ${names/(#m)*/${(P)MATCH}}

Or this, if you need to retain empty elements:

  "${(@)names/(#m)*/${(P)MATCH}}"

I don't know if there is a better way.

Roman.


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

* Re: Expand each array element with ${(P)...}
  2024-02-06  7:25 ` Roman Perepelitsa
@ 2024-02-06  7:40   ` Lawrence Velázquez
  0 siblings, 0 replies; 3+ messages in thread
From: Lawrence Velázquez @ 2024-02-06  7:40 UTC (permalink / raw)
  To: Roman Perepelitsa, Marlon Richert; +Cc: zsh-users

On Tue, Feb 6, 2024, at 2:25 AM, Roman Perepelitsa wrote:
> I don't know if there is a better way.

I'm not sure there is.  If you nest (P) to delay its application,
the shell explicitly complains about the very thing Marlon wants
to do:

	% names=(a b c)
	% : ${${(P)names}}
	zsh: parameter name reference used with array

-- 
vq


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

end of thread, other threads:[~2024-02-06  7:41 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-02-06  6:50 Expand each array element with ${(P)...} Marlon Richert
2024-02-06  7:25 ` Roman Perepelitsa
2024-02-06  7:40   ` 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).