* copying an array
@ 2014-08-05 5:44 Dominik Vogt
2014-08-05 15:41 ` Yuya Amemiya
2014-08-05 15:55 ` Bart Schaefer
0 siblings, 2 replies; 5+ messages in thread
From: Dominik Vogt @ 2014-08-05 5:44 UTC (permalink / raw)
To: Zsh Users
I'm trying to write a function that
* takes the name of a new array variable as its argument,
* creates a global array variable with the given name
* gets a copy of the values of another global array.
Sketch of the function:
--
typeset -g -a G
G=("a b c" d e)
copy_array () {
typeset -g -a "$1"
# how can this be done?
$1="($G[@])"
}
--
The function needs to handle whitespace (or any special
characters) in the values of G gracefully. The closest I've got so
far is
eval $(echo "$1=($G[@])")
But this fails to handle whitespace properly.
Bonus points if this can be done without eval. :-)
Any ideas?
Ciao
Dominik ^_^ ^_^
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: copying an array
2014-08-05 5:44 copying an array Dominik Vogt
@ 2014-08-05 15:41 ` Yuya Amemiya
2014-08-05 15:55 ` Bart Schaefer
1 sibling, 0 replies; 5+ messages in thread
From: Yuya Amemiya @ 2014-08-05 15:41 UTC (permalink / raw)
To: vogt; +Cc: zsh-users
Hi,
Try this:
eval $1'=("${G[@]}")'
If the value of $1 is foo, this is substituted with eval foo=("${G[@]}") and
evaluate it.
regards
From: Dominik Vogt <vogt@linux.vnet.ibm.com>
Subject: copying an array
Date: Tue, 5 Aug 2014 06:44:34 +0100
> I'm trying to write a function that
>
> * takes the name of a new array variable as its argument,
> * creates a global array variable with the given name
> * gets a copy of the values of another global array.
>
> Sketch of the function:
> --
> typeset -g -a G
> G=("a b c" d e)
> copy_array () {
> typeset -g -a "$1"
> # how can this be done?
> $1="($G[@])"
> }
> --
>
> The function needs to handle whitespace (or any special
> characters) in the values of G gracefully. The closest I've got so
> far is
>
> eval $(echo "$1=($G[@])")
>
> But this fails to handle whitespace properly.
>
> Bonus points if this can be done without eval. :-)
>
> Any ideas?
>
> Ciao
>
> Dominik ^_^ ^_^
>
>
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: copying an array
2014-08-05 5:44 copying an array Dominik Vogt
2014-08-05 15:41 ` Yuya Amemiya
@ 2014-08-05 15:55 ` Bart Schaefer
2014-08-08 5:12 ` Dominik Vogt
1 sibling, 1 reply; 5+ messages in thread
From: Bart Schaefer @ 2014-08-05 15:55 UTC (permalink / raw)
To: vogt, Zsh Users
On Aug 5, 6:44am, Dominik Vogt wrote:
>
> typeset -g -a G
> G=("a b c" d e)
> copy_array () {
> typeset -g -a "$1"
> # how can this be done?
> $1="($G[@])"
> }
You probably don't need the function at all if this is the only thing it
is doing:
set -A "$1" "$G[@]"
The only reason for the "typeset -g" would be if there is a local array
in scope whose name is the same as the value of $1. Otherwise "set -A"
will create the global array for you.
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: copying an array
2014-08-05 15:55 ` Bart Schaefer
@ 2014-08-08 5:12 ` Dominik Vogt
2014-08-08 6:07 ` Bart Schaefer
0 siblings, 1 reply; 5+ messages in thread
From: Dominik Vogt @ 2014-08-08 5:12 UTC (permalink / raw)
To: Zsh Users
On Tue, Aug 05, 2014 at 08:55:27AM -0700, Bart Schaefer wrote:
> On Aug 5, 6:44am, Dominik Vogt wrote:
> >
> > typeset -g -a G
> > G=("a b c" d e)
> > copy_array () {
> > typeset -g -a "$1"
> > # how can this be done?
> > $1="($G[@])"
> > }
>
> You probably don't need the function at all if this is the only thing it
> is doing:
>
> set -A "$1" "$G[@]"
Ah, I did not have "set" on the radar. Thanks!
> The only reason for the "typeset -g" would be if there is a local array
> in scope whose name is the same as the value of $1. Otherwise "set -A"
> will create the global array for you.
Is there a specific reason why typeset cannot initialize arrays?
Ciao
Dominik ^_^ ^_^
--
Dominik Vogt
IBM Germany
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: copying an array
2014-08-08 5:12 ` Dominik Vogt
@ 2014-08-08 6:07 ` Bart Schaefer
0 siblings, 0 replies; 5+ messages in thread
From: Bart Schaefer @ 2014-08-08 6:07 UTC (permalink / raw)
To: vogt, Zsh Users
[-- Attachment #1: Type: text/plain, Size: 574 bytes --]
On Thu, Aug 7, 2014 at 10:12 PM, Dominik Vogt <vogt@linux.vnet.ibm.com>
wrote:
>
> Is there a specific reason why typeset cannot initialize arrays?
>
In zsh, "typeset" is not a syntactic keyword, it's just an ordinary builtin
command. So its arguments must follow the standard metacharacter,
word-break, and quoting rules that apply to the arguments of any command.
Parenthesized array-assignment syntax can't be used, and unlike "set" the
arguments of "typeset" after the first are already given different
semantics so they can't be interpreted as values of the array.
^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2014-08-08 6:07 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2014-08-05 5:44 copying an array Dominik Vogt
2014-08-05 15:41 ` Yuya Amemiya
2014-08-05 15:55 ` Bart Schaefer
2014-08-08 5:12 ` Dominik Vogt
2014-08-08 6:07 ` 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).