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

Re: When (K) hash subscript flag could be useful?



On 2 października 2017 at 18:54:37, Bart Schaefer (schaefer@xxxxxxxxxxxxxxxx) 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



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