Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Refering to overlaid variable from within a function

Context: I have some functions for manipulating sets.  A couple of
examples are at the bottom of this mail.  Using those, I can do:
% newset aa foo bar
% newset bb bar baz
% set_subtract_in aa bb
% echo $aa

For manipulating large lists of machines, retrieved from various
sources, this is invaluable.  As you can see from the context, I need to
pass around the names of the arrays, rather than the array contents.

The problem I have is that I just noticed that I can't use sets named
"a", "b", "c", "name" because those are variables local in the suite of
set manipulation functions.

Is there a way to refer to variables as they exist in the context of the
caller of a function, so that you can use a name but not have to worry
about the caller's names?

Otherwise, my options are to not name the sets inside the functions,
which gets even harder to read, or to use __setmanip_<varname>s and just
rely on that not conflicting, but I'm wondering if there's a cleaner way
of doing this?


function newset {
        setopt local_options no_ksh_arrays
        local name="$1"; shift
        typeset -gUa $name
        set -A $name "$@"

function set_subtract_in {
        setopt local_options no_ksh_arrays
        local name="$1" b="$2"
        set -A $name "${(P@)name:#${(P~j,|,)b}}"

Messages sorted by: Reverse Date, Date, Thread, Author