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

Re: ${(kv)foo[bar]}



On Apr 3,  2:35pm, Andrey Borzenkov wrote:
} Subject: ${(kv)foo[bar]}
}
} print ${(kv)foo[bar]}
} 
} should print "bar baz". Apparently it repsects only one flag in this case.

That's why the doc for each of (k) and (v) has a special clause "Used with
subscripts, ..." although I admit it could be clearer.  This is actually a
side-effect of the way subscripting was implemented before assocs even
existed -- when the subscript is not a pattern form, the lower-level code
is optimized [for lack of a better word] to avoid returning an array, so
there's no way for both (k) and (v) to operate at once.  Since, in this
variation, you must already know the subscript for (k), it's most useful
if the scalar that's returned is the value for (v).

} Having it working would be handy e.g. in _arguments callbacks that
} need to get options from command line; e.g. (current _urpmi)
} 
}       pkgs=( $(urpmq --list
}                 ${(k)opt_args[--media]} ${(v)opt_args[--media]}
}                 ${(k)opt_args[--searchmedia]} ${(v)opt_args[--searchmedia]}
}                 2> /dev/null
}            )

Isn't that a rather silly way to do it in any case?  You don't need the
subscript flags at all.  Why not

      pkgs=( $(urpmq --list
                --media ${opt_args[--media]}
                --searchmedia ${opt_args[--searchmedia]}
                2> /dev/null
           )

If for some reason you want to be deliberately obscure, you can force it
with:

      pkgs=( $(urpmq --list
                ${(kv)opt_args[(i)--media]}
                ${(kv)opt_args[(i)--searchmedia]}
                2> /dev/null
           )

The use of the (i) pattern-matching operator sends the subscript lookup
through a different branch of the code where it's possible to return an
array.  Heck, you could even do:

      pkgs=( $(urpmq --list
                ${(kv)opt_args[(I)--*media]}
                2> /dev/null
           )



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