zsh-users
 help / color / mirror / code / Atom feed
* print entire array *with* subscript?
@ 2006-02-09 19:40 Jean-Rene David
  0 siblings, 0 replies; 5+ messages in thread
From: Jean-Rene David @ 2006-02-09 19:40 UTC (permalink / raw)
  To: zsh-users

The (k) expansion flag for associative arrays
substitutes the keys of the array instead of the
values.

But when applied to a normal array, it does
nothing. I thought it would have printed the
values' indices. It seems like an intuitive thing
to do. I thought that's what was meant in the
documentation:

13.3.1 Parameter Expansion Flags
[...]

k
   [...] Used with subscripts (including
   ordinary arrays), force indices or keys to be
   substituted even if the subscript form refers to
   values. [...]

Either I misunderstand, or it's not doing what it
says. I lean towards the former. Can someone shed
light?

$ zsh --version
zsh 4.2.5 (i686-pc-linux-gnu)

-- 
JR


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

* Re: print entire array *with* subscript?
  2006-02-11  4:15     ` Justin M Wozniak
@ 2006-02-11  5:20       ` Bart Schaefer
  0 siblings, 0 replies; 5+ messages in thread
From: Bart Schaefer @ 2006-02-11  5:20 UTC (permalink / raw)
  To: Justin M Wozniak, zsh-users

On Feb 10, 11:15pm, Justin M Wozniak wrote:
} 
} In your solution, what is the point of (r) ?

Subscript Flags
---------------

If the opening bracket, or the comma in a range, in any subscript
expression is directly followed by an opening parenthesis, the string up
to the matching closing one is considered to be a list of flags, as in
`NAME[(FLAGS)EXP]'.  The flags currently understood are:

 ...

r
     Reverse subscripting: if this flag is given, the EXP is taken as a
     pattern and the result is the first matching array element,
     substring or word (if the parameter is an array, if it is a
     scalar, or if it is a scalar and the `w' flag is given,
     respectively). ...
 


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

* Re: print entire array *with* subscript?
  2006-02-10 18:03   ` Peter Stephenson
@ 2006-02-11  4:15     ` Justin M Wozniak
  2006-02-11  5:20       ` Bart Schaefer
  0 siblings, 1 reply; 5+ messages in thread
From: Justin M Wozniak @ 2006-02-11  4:15 UTC (permalink / raw)
  To: zsh-users


In your solution, what is the point of (r) ?

-- 
Justin Michael Joseph Wozniak

On Fri, 10 Feb 2006, Peter Stephenson wrote:

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


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

* Re: print entire array *with* subscript?
  2006-02-10 17:50 ` Jean-Rene David
@ 2006-02-10 18:03   ` Peter Stephenson
  2006-02-11  4:15     ` Justin M Wozniak
  0 siblings, 1 reply; 5+ messages in thread
From: Peter Stephenson @ 2006-02-10 18:03 UTC (permalink / raw)
  To: zsh-users

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@csr.com>                  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


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

* Re: print entire array *with* subscript?
       [not found] <200602101756.59858.arvidjaar@mail.ru>
@ 2006-02-10 17:50 ` Jean-Rene David
  2006-02-10 18:03   ` Peter Stephenson
  0 siblings, 1 reply; 5+ messages in thread
From: Jean-Rene David @ 2006-02-10 17:50 UTC (permalink / raw)
  To: zsh-users

* Andrey Borzenkov <arvidjaar@mail.ru>:
> but I really fail to see when it can be useful. After all to print all array 
> indices (ignoring KSH_ARRAYS for the moment):
> 
> {pts/0}% echo {1..$#foo}
> 1 2 3

The usefulness is a little more obvious when
printing both indices and values. 

The original idea was this.  Suppose I have a list
of files in an array and I want to select one to
do a command on.

files=(foo.h foo.c bar.h bar.c)

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.

Now that I wrote all this I realize I could just
use 'select'...

-- 
JR


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

end of thread, other threads:[~2006-02-11  5:20 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2006-02-09 19:40 print entire array *with* subscript? Jean-Rene David
     [not found] <200602101756.59858.arvidjaar@mail.ru>
2006-02-10 17:50 ` Jean-Rene David
2006-02-10 18:03   ` Peter Stephenson
2006-02-11  4:15     ` Justin M Wozniak
2006-02-11  5:20       ` Bart Schaefer

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