zsh-workers
 help / color / mirror / code / Atom feed
* ${(kv)foo[bar]}
@ 2005-04-03 10:35 Andrey Borzenkov
  2005-04-03 17:49 ` ${(kv)foo[bar]} Bart Schaefer
  0 siblings, 1 reply; 4+ messages in thread
From: Andrey Borzenkov @ 2005-04-03 10:35 UTC (permalink / raw)
  To: zsh-workers

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

I was under impression that

typeset -A foo
foo[bar]=baz
print ${(kv)foo[bar]}

should print "bar baz". Apparently it repsects only one flag in this case.

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
           )

- -andrey
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.0 (GNU/Linux)

iD8DBQFCT8bnR6LMutpd94wRAh8bAKC8nVCX7fMQkc6vDSJgq8McMYeyJwCeISWp
SwlkICW2j5idqVcAGoC54Gc=
=Gvn/
-----END PGP SIGNATURE-----


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: ${(kv)foo[bar]}
  2005-04-03 10:35 ${(kv)foo[bar]} Andrey Borzenkov
@ 2005-04-03 17:49 ` Bart Schaefer
  2005-04-03 18:00   ` ${(kv)foo[bar]} Andrey Borzenkov
  0 siblings, 1 reply; 4+ messages in thread
From: Bart Schaefer @ 2005-04-03 17:49 UTC (permalink / raw)
  To: Andrey Borzenkov, zsh-workers

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
           )


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: ${(kv)foo[bar]}
  2005-04-03 17:49 ` ${(kv)foo[bar]} Bart Schaefer
@ 2005-04-03 18:00   ` Andrey Borzenkov
  2005-04-03 19:58     ` ${(kv)foo[bar]} Bart Schaefer
  0 siblings, 1 reply; 4+ messages in thread
From: Andrey Borzenkov @ 2005-04-03 18:00 UTC (permalink / raw)
  To: zsh-workers

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

On Sunday 03 April 2005 21:49, Bart Schaefer wrote:
>
> 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
>            )
>

because it breaks if no --media is given on command line. My example would 
have worked in both cases.

> 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
>            )
>

OK, in my case it turned out I needed something more elaborate anyway but 
thank's for the idea, I'll keep it in mind :)

Although it is rather hard to understand why ${(kv)opt_args[(i)--media]} 
should work and ${(kv)opt_args[--media]} not.

- -andrey
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.0 (GNU/Linux)

iD8DBQFCUC9JR6LMutpd94wRAimbAJ9bmX5OGhZAWmDgJREeHrkyFhuDNACdE7yU
c2ROqzFKiCBtUa0aoJaKs9s=
=EvN6
-----END PGP SIGNATURE-----


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: ${(kv)foo[bar]}
  2005-04-03 18:00   ` ${(kv)foo[bar]} Andrey Borzenkov
@ 2005-04-03 19:58     ` Bart Schaefer
  0 siblings, 0 replies; 4+ messages in thread
From: Bart Schaefer @ 2005-04-03 19:58 UTC (permalink / raw)
  To: zsh-workers

On Apr 3, 10:00pm, Andrey Borzenkov wrote:
} Subject: Re: ${(kv)foo[bar]}
}
} Although it is rather hard to understand why ${(kv)opt_args[(i)--media]} 
} should work and ${(kv)opt_args[--media]} not.

The rationalization is that with [(i)...] the subscript is a pattern, so
you may not know in advance what it's going to match, and therefore it
is useful to be able to return both the key and the value.  [(i)--media]
is a degenerate case where the pattern is a plain string.  Without the
(i), the subscript is always a plain string and therefore known, and you
don't need to be able to expand it.

The real reason, as I said, is that the implementation of subscripting
with [(i)...] does a scan of the hash keys rather than a simple hash
lookup; it's got code that's shared with (R) and (I) and therefore is
designed to return an array.  Implementing subscripting with a full
scan in the ordinary case just for (kv) would foil the reason for using
a hash table to begin with.


^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2005-04-03 19:59 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2005-04-03 10:35 ${(kv)foo[bar]} Andrey Borzenkov
2005-04-03 17:49 ` ${(kv)foo[bar]} Bart Schaefer
2005-04-03 18:00   ` ${(kv)foo[bar]} Andrey Borzenkov
2005-04-03 19:58     ` ${(kv)foo[bar]} 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).