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

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



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.



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