* Passing array to function
@ 2016-09-30 7:15 Ignatius Reilly
0 siblings, 0 replies; 4+ messages in thread
From: Ignatius Reilly @ 2016-09-30 7:15 UTC (permalink / raw)
To: zsh-users
I have the following function:
addtoarray() { [[ -d $1 ]] && myarray=($1 $myarray) }
which works as expected, prepending an element to an array only if its an
existing directory. I'd like to rewrite this function so that I can pass
the array name as a parameter like so:
addtoarray /usr/foo myarray
but I can't figure out how. I've tried everything I can think of without
any success.
Thanks
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: Passing array to function
2016-09-30 7:45 ` Daniel Shahaf
2016-09-30 8:28 ` Mikael Magnusson
@ 2016-09-30 18:26 ` Bart Schaefer
1 sibling, 0 replies; 4+ messages in thread
From: Bart Schaefer @ 2016-09-30 18:26 UTC (permalink / raw)
To: zsh-users
On Sep 30, 7:45am, Daniel Shahaf wrote:
} Subject: Re: Passing array to function
}
} Ignatius Reilly wrote on Fri, Sep 30, 2016 at 02:15:55 -0500:
} > addtoarray() { [[ -d $1 ]] && myarray=($1 $myarray) }
} >
} > addtoarray /usr/foo myarray
}
} addtoarray() {
} [[ -d $1 ]] && eval "${(q)2}[1,0]=${(q)1}"
} }
Solution without eval:
addtoarray() { [[ -d $1 ]] && set -A $2 $1 ${(P)2} }
Various quoting may be necessary if you using an emulation mode or
other nonstandard setopts, but from your original example it does
not appear so. ${(P)2} means to treat the value of $2 as a name
and expand that named parameter.
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: Passing array to function
2016-09-30 7:45 ` Daniel Shahaf
@ 2016-09-30 8:28 ` Mikael Magnusson
2016-09-30 18:26 ` Bart Schaefer
1 sibling, 0 replies; 4+ messages in thread
From: Mikael Magnusson @ 2016-09-30 8:28 UTC (permalink / raw)
To: Daniel Shahaf; +Cc: Ignatius Reilly, Zsh Users
On Fri, Sep 30, 2016 at 9:45 AM, Daniel Shahaf <d.s@daniel.shahaf.name> wrote:
> Ignatius Reilly wrote on Fri, Sep 30, 2016 at 02:15:55 -0500:
>> addtoarray() { [[ -d $1 ]] && myarray=($1 $myarray) }
>>
>> which works as expected, prepending an element to an array only if its an
>> existing directory. I'd like to rewrite this function so that I can pass
>> the array name as a parameter like so:
>>
>> addtoarray /usr/foo myarray
>
> You could do it with eval:
>
> addtoarray() {
> [[ -d $1 ]] && eval "${(q)2}[1,0]=${(q)1}"
> }
>
> Explanation:
>
> - The (q) are there to convert the values to command-line-quoted
> strings, for eval. $2 probably needs no quoting — if it did, the eval
> would see a syntax error — but I put the (q) anyway to guard against
> invalid inputs (bobby tables attacks against the eval).
>
> - After parameter substitution, the resultant string is:
> myarray[1,0]=/usr/foo
> which is a slice assignment that prepends an element to the named
> array.
>
> If there's a solution without eval I'm sure someone will post it.
addtoarray() { [[ -d $1 ]] || return; local p="$2[1,0]"; : ${(P)p::=$1} }
I forgot about [1,0] yesterday when you asked on IRC.
--
Mikael Magnusson
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: Passing array to function
[not found] <1A643BF6-28C9-483E-9312-D8C856D41F0D__26800.772539764$1475219850$gmane$org@gmail.com>
@ 2016-09-30 7:45 ` Daniel Shahaf
2016-09-30 8:28 ` Mikael Magnusson
2016-09-30 18:26 ` Bart Schaefer
0 siblings, 2 replies; 4+ messages in thread
From: Daniel Shahaf @ 2016-09-30 7:45 UTC (permalink / raw)
To: Ignatius Reilly; +Cc: zsh-users
Ignatius Reilly wrote on Fri, Sep 30, 2016 at 02:15:55 -0500:
> addtoarray() { [[ -d $1 ]] && myarray=($1 $myarray) }
>
> which works as expected, prepending an element to an array only if its an
> existing directory. I'd like to rewrite this function so that I can pass
> the array name as a parameter like so:
>
> addtoarray /usr/foo myarray
You could do it with eval:
addtoarray() {
[[ -d $1 ]] && eval "${(q)2}[1,0]=${(q)1}"
}
Explanation:
- The (q) are there to convert the values to command-line-quoted
strings, for eval. $2 probably needs no quoting — if it did, the eval
would see a syntax error — but I put the (q) anyway to guard against
invalid inputs (bobby tables attacks against the eval).
- After parameter substitution, the resultant string is:
myarray[1,0]=/usr/foo
which is a slice assignment that prepends an element to the named
array.
If there's a solution without eval I'm sure someone will post it.
Cheers,
Daniel
> Thanks
>
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2016-09-30 19:07 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2016-09-30 7:15 Passing array to function Ignatius Reilly
[not found] <1A643BF6-28C9-483E-9312-D8C856D41F0D__26800.772539764$1475219850$gmane$org@gmail.com>
2016-09-30 7:45 ` Daniel Shahaf
2016-09-30 8:28 ` Mikael Magnusson
2016-09-30 18:26 ` 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).