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

Re: Associative array ordering and selective unset (Re: Example function)



Bart Schaefer wrote:

> (Note that I moved the (qQ) into the subscript flags, which is probably
> where it really has to be.  And I still hope for a better letter.)
> 
> Now change the assignment a little:
> 
>         map=('*.(gz|Z)' ': 1; zcat
>              '*.bz2' ': 2; bzip2 -dc'
>              '*.bz' ': 3; bzip -dc'
>              '*' ': 4; <')
> 
> Now we can use the (o) substitution flag like so:
> 
> 	eval ${${(o)map[(Q)$argv[i]]}[1]} '$argv[i]'
> 
> That says "find all the values for which the key is a pattern that matches
> $argv[i], sort them in ascending order, take the first one, and evaluate
> it as a command with argument $argv[i]."  By embedding the "preferred"
> ordering in the value as a leading ":" command, we're assured of always
> getting the most-specific match.

Hmm, I like this. For bigger arrays one would have to use `01' and so
on, but this should be acceptible.

Bye
 Sven


--
Sven Wischnowsky                         wischnow@xxxxxxxxxxxxxxxxxxxxxxx



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