zsh-users
 help / color / mirror / code / Atom feed
* array as loopvariable - howto
@ 2003-05-08  9:58 jarausch
  2003-05-08 10:24 ` Borzenkov Andrey
                   ` (2 more replies)
  0 siblings, 3 replies; 6+ messages in thread
From: jarausch @ 2003-05-08  9:58 UTC (permalink / raw)
  To: zsh-users

Hi

I would like to write a loop over a list of arrays,

e.g. I would like to iterate over pairs of (username,UID)

A naiv try like

for User in (User1,100) (User2,120); do  echo "$U[1] has UID $U[2]";
done

failed.
Is this possible in ZSH?

Many thanks for a hint,

Helmut Jarausch

Lehrstuhl fuer Numerische Mathematik
RWTH - Aachen University
D 52056 Aachen, Germany


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

* RE: array as loopvariable - howto
  2003-05-08  9:58 array as loopvariable - howto jarausch
@ 2003-05-08 10:24 ` Borzenkov Andrey
  2003-05-08 10:33 ` Peter Stephenson
  2003-05-11 18:15 ` array as loopvariable - howto Bart Schaefer
  2 siblings, 0 replies; 6+ messages in thread
From: Borzenkov Andrey @ 2003-05-08 10:24 UTC (permalink / raw)
  To: jarausch, zsh-users


> 
> I would like to write a loop over a list of arrays,
> 
> e.g. I would like to iterate over pairs of (username,UID)
> 
> A naiv try like
> 
> for User in (User1,100) (User2,120); do  echo "$U[1] has UID $U[2]";
> done
> 
> failed.
> Is this possible in ZSH?
> 



For array variables what is wrong with

A=(user1 uid1 ...)
B=(user2 uid2 ...)

for user uid in $A $B; do
  echo User $user has $uid
done

for literal arrays just put them all in for loop

for user uid in user1 250 user2 350 ...

Or I do not understand your question?

-andrey


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

* Re: array as loopvariable - howto
  2003-05-08  9:58 array as loopvariable - howto jarausch
  2003-05-08 10:24 ` Borzenkov Andrey
@ 2003-05-08 10:33 ` Peter Stephenson
  2003-05-08 10:50   ` Peter Stephenson
  2003-05-08 12:14   ` Upcoming (Was: array as loopvariable - howto) Geoff Wing
  2003-05-11 18:15 ` array as loopvariable - howto Bart Schaefer
  2 siblings, 2 replies; 6+ messages in thread
From: Peter Stephenson @ 2003-05-08 10:33 UTC (permalink / raw)
  To: zsh-users

jarausch@igpm.rwth-aachen.de wrote:
> I would like to write a loop over a list of arrays,
> 
> e.g. I would like to iterate over pairs of (username,UID)
> 
> A naiv try like
> 
> for User in (User1,100) (User2,120); do  echo "$U[1] has UID $U[2]";
> done
> 
> failed.
> Is this possible in ZSH?

>From 4.1.1 which is appearing Real Soon Now (TM) you can do

for User Uid in User1 100 User2 120; do
  echo "$User has UID $User2"
done

(You are encouraged to try out 4.1.1-test-2 if you are at all interested
in upgrading.)

Until then, you are stuck with either indexing multiple real arrays or
tricks with word splitting.

-- 
Peter Stephenson <pws@csr.com>                  Software Engineer
CSR Ltd., Science Park, Milton Road,
Cambridge, CB4 0WH, UK                          Tel: +44 (0)1223 692070


**********************************************************************
The information transmitted is intended only for the person or
entity to which it is addressed and may contain confidential 
and/or privileged material. 
Any review, retransmission, dissemination or other use of, or
taking of any action in reliance upon, this information by 
persons or entities other than the intended recipient is 
prohibited.  
If you received this in error, please contact the sender and 
delete the material from any computer.
**********************************************************************


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

* Re: array as loopvariable - howto
  2003-05-08 10:33 ` Peter Stephenson
@ 2003-05-08 10:50   ` Peter Stephenson
  2003-05-08 12:14   ` Upcoming (Was: array as loopvariable - howto) Geoff Wing
  1 sibling, 0 replies; 6+ messages in thread
From: Peter Stephenson @ 2003-05-08 10:50 UTC (permalink / raw)
  To: zsh-users

Peter Stephenson wrote:
> for User Uid in User1 100 User2 120; do
>   echo "$User has UID $User2"
                        ^^^^^^ This should be $Uid, obviously, sorry.
> done

-- 
Peter Stephenson <pws@csr.com>                  Software Engineer
CSR Ltd., Science Park, Milton Road,
Cambridge, CB4 0WH, UK                          Tel: +44 (0)1223 692070


**********************************************************************
The information transmitted is intended only for the person or
entity to which it is addressed and may contain confidential 
and/or privileged material. 
Any review, retransmission, dissemination or other use of, or
taking of any action in reliance upon, this information by 
persons or entities other than the intended recipient is 
prohibited.  
If you received this in error, please contact the sender and 
delete the material from any computer.
**********************************************************************


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

* Upcoming (Was: array as loopvariable - howto)
  2003-05-08 10:33 ` Peter Stephenson
  2003-05-08 10:50   ` Peter Stephenson
@ 2003-05-08 12:14   ` Geoff Wing
  1 sibling, 0 replies; 6+ messages in thread
From: Geoff Wing @ 2003-05-08 12:14 UTC (permalink / raw)
  To: zsh-users

Peter Stephenson <pws@csr.com> typed:
: From 4.1.1 which is appearing Real Soon Now (TM) you can do

Is this a promise?  Do you have holidays planned?  :-)

Regards,
Geoff


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

* Re: array as loopvariable - howto
  2003-05-08  9:58 array as loopvariable - howto jarausch
  2003-05-08 10:24 ` Borzenkov Andrey
  2003-05-08 10:33 ` Peter Stephenson
@ 2003-05-11 18:15 ` Bart Schaefer
  2 siblings, 0 replies; 6+ messages in thread
From: Bart Schaefer @ 2003-05-11 18:15 UTC (permalink / raw)
  To: zsh-users

On May 8, 11:58am, jarausch@igpm.rwth-aachen.de wrote:
} 
} for User in (User1,100) (User2,120); do  echo "$U[1] has UID $U[2]";
} done

I've seen responses on how to do this with the new multi-variable "for"
syntax in 4.1, but nothing about how to do it in 4.0.

I'd say the obvious solution is an associative array.

4.1 multi-variable "for":

for user uid in User1 100 User2 120 ...; do echo $user has UID $uid; done

4.0 or 4.1 associative array:

typeset -A umap; umap=( User1 100 User2 120 ... )
for user in ${(k)umap}; do echo $user has UID $umap[$user]; done


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

end of thread, other threads:[~2003-05-11 18:15 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2003-05-08  9:58 array as loopvariable - howto jarausch
2003-05-08 10:24 ` Borzenkov Andrey
2003-05-08 10:33 ` Peter Stephenson
2003-05-08 10:50   ` Peter Stephenson
2003-05-08 12:14   ` Upcoming (Was: array as loopvariable - howto) Geoff Wing
2003-05-11 18:15 ` array as loopvariable - howto 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).