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

Re: print entire array *with* subscript?



Jean-Rene David wrote:
> It would be nice to be able to do this:
> 
> print ${(k)files}
> 1 foo.h 2 foo.c 3 bar.h 4 bar.c
> 
> instead of:
> 
> for f in {1..$#files}; do 
>   print $f ${files[$f]};
> done
> 
> Not a big deal. Just asking because that's what I
> thought the documentation meant.

I think the documentation for the (k) flag is (for once) reasonably
clear and accurate:

       k      If  name  refers  to  an  associative array, substitute the keys
              (element names) rather than the values of  the  elements.   Used
              with  subscripts  (including  ordinary arrays), force indices or
              keys to be substituted even if the subscript form refers to val-
              ues.   However,  this  flag  may  not be combined with subscript
              ranges.

This means you can use it with normal arrays, but only with individual
indices.  So:

% print ${(k)files[(r)foo.c]}
2

but it doesn't work with a subscript nor with a range nor with the *
and @ subscripts (which are special ranges).

That doesn't mean this is convenient...

-- 
Peter Stephenson <pws@xxxxxxx>                  Software Engineer
CSR PLC, Churchill House, Cambridge Business Park, Cowley Road
Cambridge, CB4 0WZ, UK                          Tel: +44 (0)1223 692070


To access the latest news from CSR copy this link into a web browser:  http://www.csr.com/email_sig.php



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