zsh-users
 help / color / mirror / code / Atom feed
* When (K) hash subscript flag could be useful?
@ 2017-10-02 13:44 Sebastian Gniazdowski
  2017-10-02 16:54 ` Bart Schaefer
  0 siblings, 1 reply; 3+ messages in thread
From: Sebastian Gniazdowski @ 2017-10-02 13:44 UTC (permalink / raw)
  To: Zsh Users

Hello,
I was stress testing the new hash assignment syntax for dash problems (because of having regrets about not doing this before -pcre_match regression revealed itself). Didn't find problems, but incidentally I stumbled upon ${harr[(K)...]}. Completion says:

K  -- all values where subscript matched by key as pattern

I finally decoded this – it takes keys of hash, treats them as patterns, and compares to subscript. So:

% local -A harr; harr=( 1\* 2- ); typeset -p1 -- harr; print -rl "${harr[(K)1-aaa]}"
typeset -A harr=(
  ['1*']=2-
)
2-

So this is reverse to what is expected: return keys, but match subscript-query to values. However passing (k) flag to match-values (R) subscript-flag does what's expected:

% local -A harr; harr=( 1- 2- ); typeset -p1 -- harr; print -rl "${(k)harr[(R)2-*]}"
typeset -A harr=(
  [1-]=2-
)
1-

I suspect (K) was added for versality. But maybe there are know applications of such "hash = database of patterns" construct?

--  
Sebastian Gniazdowski
psprint /at/ zdharma.org


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

* Re: When (K) hash subscript flag could be useful?
  2017-10-02 13:44 When (K) hash subscript flag could be useful? Sebastian Gniazdowski
@ 2017-10-02 16:54 ` Bart Schaefer
  2017-10-02 17:51   ` Sebastian Gniazdowski
  0 siblings, 1 reply; 3+ messages in thread
From: Bart Schaefer @ 2017-10-02 16:54 UTC (permalink / raw)
  To: Zsh Users

On Oct 2,  3:44pm, Sebastian Gniazdowski wrote:
}
} I stumbled upon ${harr[(K)...]}. Completion says:
} 
} K  -- all values where subscript matched by key as pattern
} 
} So this is reverse to what is expected

Er, expected by who, why?

} I suspect (K) was added for versality. But maybe there are know
} applications of such "hash = database of patterns" construct?

It allows you to build something like a "case" statement where the
branches aren't established until run time.  Instead of

  case $x in
  (...) y=$z;;
  # ... etc. ...
  esac

you can write

   y=${z[(K)$x]}

It's also basically the same thing that "zstyle" does with context
lookup, except that zstyle layers on special treatment of using the
"best match" to always return a single result.


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

* Re: When (K) hash subscript flag could be useful?
  2017-10-02 16:54 ` Bart Schaefer
@ 2017-10-02 17:51   ` Sebastian Gniazdowski
  0 siblings, 0 replies; 3+ messages in thread
From: Sebastian Gniazdowski @ 2017-10-02 17:51 UTC (permalink / raw)
  To: Bart Schaefer, Zsh Users

On 2 października 2017 at 18:54:37, Bart Schaefer (schaefer@brasslantern.com) wrote:
> On Oct 2, 3:44pm, Sebastian Gniazdowski wrote:
> }
> } I stumbled upon ${harr[(K)...]}. Completion says:
> }
> } K -- all values where subscript matched by key as pattern
> }
> } So this is reverse to what is expected
>  
> Er, expected by who, why?

I was trying to think like a regular user, when he reads about (R) for value matching, (I) for key matching, then (K) can be I think expected to be some combination of the two previous straightforward ones.
 
> It allows you to build something like a "case" statement where the
> branches aren't established until run time. Instead of
>  
> case $x in
> (...) y=$z;;
> # ... etc. ...
> esac
>  
> you can write
>  
> y=${z[(K)$x]}

Interesting, one could even skip "y=" and call handler function, but there is a problem, undefined order, so final catch-all "*" key could be called first. Just thinking about performance of e.g. syntax-highlighting, there are large case statements there, however function call uses significant CPU time (when in long loop), so there would probably be no gain anyway.

I wasn't criticizing (K) but thinking that it's a cool tool that can solve some crucial problem(s).
--  
Sebastian Gniazdowski
psprint /at/ zdharma.org


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

end of thread, other threads:[~2017-10-02 17:51 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2017-10-02 13:44 When (K) hash subscript flag could be useful? Sebastian Gniazdowski
2017-10-02 16:54 ` Bart Schaefer
2017-10-02 17:51   ` Sebastian Gniazdowski

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