zsh-workers
 help / color / mirror / code / Atom feed
From: Bart Schaefer <schaefer@brasslantern.com>
To: Andrey Borzenkov <arvidjaar@newmail.ru>, zsh-workers@sunsite.dk
Subject: Re: ${(kv)foo[bar]}
Date: Sun, 03 Apr 2005 17:49:03 +0000	[thread overview]
Message-ID: <1050403174903.ZM4196@candle.brasslantern.com> (raw)
In-Reply-To: <200504031435.19457.arvidjaar@newmail.ru>

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
           )


  reply	other threads:[~2005-04-03 17:49 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2005-04-03 10:35 ${(kv)foo[bar]} Andrey Borzenkov
2005-04-03 17:49 ` Bart Schaefer [this message]
2005-04-03 18:00   ` ${(kv)foo[bar]} Andrey Borzenkov
2005-04-03 19:58     ` ${(kv)foo[bar]} Bart Schaefer

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1050403174903.ZM4196@candle.brasslantern.com \
    --to=schaefer@brasslantern.com \
    --cc=arvidjaar@newmail.ru \
    --cc=zsh-workers@sunsite.dk \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).