zsh-users
 help / color / mirror / code / Atom feed
* Iterating over a set of arrays
@ 2014-06-09 10:05 Thorsten Kampe
  2014-06-09 10:20 ` Peter Stephenson
  0 siblings, 1 reply; 3+ messages in thread
From: Thorsten Kampe @ 2014-06-09 10:05 UTC (permalink / raw)
  To: zsh-users

Hi,

this is originally a bash question but I'd also be interested in a 
zsh solution (or if there is no solution).

I'd like to get the second element of a set of arrays.

arrayA=(a1 a2 a3)
arrayB=(b1 b2 b3)
arrayC=(c1 c2 c3)

The output should be

a2
b2
c2

To avoid repetitive code, I'd like to do this in a loop. 

The below does not work because iterating over an array iterates over 
the elements and there is no array of arrays in bash and zsh

for array in ${arrayA[@]} ${arrayB[@]}
do
	echo ${array[1]}
done

So is there a solution to the above problem?

Thorsten


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

* Re: Iterating over a set of arrays
  2014-06-09 10:05 Iterating over a set of arrays Thorsten Kampe
@ 2014-06-09 10:20 ` Peter Stephenson
  2014-06-09 19:09   ` zzapper
  0 siblings, 1 reply; 3+ messages in thread
From: Peter Stephenson @ 2014-06-09 10:20 UTC (permalink / raw)
  To: zsh-users

On Mon, 09 Jun 2014 12:05:51 +0200
Thorsten Kampe <thorsten@thorstenkampe.de> wrote:
> this is originally a bash question but I'd also be interested in a 
> zsh solution (or if there is no solution).
> 
> I'd like to get the second element of a set of arrays.
> 
> arrayA=(a1 a2 a3)
> arrayB=(b1 b2 b3)
> arrayC=(c1 c2 c3)
> 
> The output should be
> 
> a2
> b2
> c2

The (P) flag does what you want.  Remember the array indexing is (by
default) different from bash.


for a in array{A,B,C}; do
  print ${${(P)a}[2]}
done


You need the extra braces to ensure the result of the name substitution
gets the index, not the original variable.

pws


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

* Re: Iterating over a set of arrays
  2014-06-09 10:20 ` Peter Stephenson
@ 2014-06-09 19:09   ` zzapper
  0 siblings, 0 replies; 3+ messages in thread
From: zzapper @ 2014-06-09 19:09 UTC (permalink / raw)
  To: zsh-users


>> 
>> arrayA=(a1 a2 a3)
>> arrayB=(b1 b2 b3)
>> arrayC=(c1 c2 c3)


> 
> for a in array{A,B,C}; do
>   print ${${(P)a}[2]}
> done
> 

setopt interactivecomments
# messing around a little with above
for a (array{A,B,C}) echo ${${(P)a}[2]}

# or in this case same thing
for a (array{A,B,C}) echo ${${(P)a}[-2]}

# or with a little formatting
for a (array{A,B,C}) echo ${${(PUl:5::.:r:5::.:)a}[-2]}




-- 
zzapper
https://twitter.com/dailyzshtip

---
This email is free from viruses and malware because avast! Antivirus protection is active.
http://www.avast.com



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

end of thread, other threads:[~2014-06-09 19:10 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2014-06-09 10:05 Iterating over a set of arrays Thorsten Kampe
2014-06-09 10:20 ` Peter Stephenson
2014-06-09 19:09   ` zzapper

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