* Writing to an indirectly named array...
@ 2012-01-01 6:09 meino.cramer
2012-01-01 6:45 ` Bart Schaefer
0 siblings, 1 reply; 3+ messages in thread
From: meino.cramer @ 2012-01-01 6:09 UTC (permalink / raw)
To: zsh-users
Hi,
HAPPY NEW YEAR! :)
With
myarray=( 1 2 3 4 5 6 7 8 9 10 )
nameofarray=myarray
echo ${(P)nameofarray}
one can read from an array which name is given by the
contents of a variable.
But how can I write to that array the same way (via 'nameofarray') ?
Thank you very much in advance for any help! :)
Best regards,
mcc
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: Writing to an indirectly named array...
2012-01-01 6:09 Writing to an indirectly named array meino.cramer
@ 2012-01-01 6:45 ` Bart Schaefer
[not found] ` <20120101065003.GD2920@solfire>
0 siblings, 1 reply; 3+ messages in thread
From: Bart Schaefer @ 2012-01-01 6:45 UTC (permalink / raw)
To: zsh-users
On Jan 1, 7:09am, meino.cramer@gmx.de wrote:
}
} But how can I write to that array the same way (via 'nameofarray') ?
The simplest way is:
set -A $nameofarray elem1 elem2 ...
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: Writing to an indirectly named array...
[not found] ` <20120101065003.GD2920@solfire>
@ 2012-01-01 19:16 ` Bart Schaefer
0 siblings, 0 replies; 3+ messages in thread
From: Bart Schaefer @ 2012-01-01 19:16 UTC (permalink / raw)
To: meino.cramer; +Cc: zsh-users
[Cc zsh-users on private reply, hope that's OK]
On Jan 1, 7:50am, meino.cramer@gmx.de wrote:
}
} > set -A $nameofarray elem1 elem2 ...
}
} ...is there another way by using the (P)-notation somehow ?
Yes, but it still doesn't get you things like array+=(elems) or
array[N]=(elems), and it's a little more difficult to get the
elements split the way you may want them.
: ${(PA)=nameofarray::=elem1 elem2 ...}
This says to dereference namaofarray and then assign the result of
the ::= operator as an array. The shwordsplit (=) operator applies
across the whole assigment so elem1 elem2 ... are split as well.
If you don't want that semantics, you can apply an explicit split
to the right side, e.g.,
print file has ${(PA)#nameofarray::=${(f)"$(<file)"}} lines
print the fifth line is ${${(P)nameofarray}[5]}
You actually can poke into the middle of an array using the (P) form
but it's rather convoluted and probably error-prone:
local thefifthelement="$nameofarray"'[5]'
: ${(PA)=thefifthelement=three new elements}
That is, you include the subscript expression in the value of the
parameter that you then dereference with (P). This works for anything
that could normally appear on the left side of an assignment.
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2012-01-01 19:17 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2012-01-01 6:09 Writing to an indirectly named array meino.cramer
2012-01-01 6:45 ` Bart Schaefer
[not found] ` <20120101065003.GD2920@solfire>
2012-01-01 19:16 ` 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).