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

Re: for loop question



2014-11-02 13:00:14 -0800, Bart Schaefer:
> > I've fiddled around with various modifications but it seems to dislike "
> [ -n "$TLC[i]" ] "
> > even though the 'while' loop is happy with it.  That expression evaluates
> to true or
> > false, does it not? So why won't 'for' swallow it?
> 
> The double parents (( )) are special arithmetic syntax.  You can't use an
> ordinary shell command like "test" ( for which "[" is an alias) inside that
> construct.

You could with:

for ((i=1; (z[$([ -n "$TLC[i]" ])0]),$? == 0; i++))
  print -ru2 -- $TLC[i]

(not that you would want to).

Here, you more likely want:

for i ("$TLC[@]") print -ru2 -- $i

or

print -rlu2 -- "$TLC[@]"

or:

for ((i = 1; i <= $#TLC; i++)) print -ru2 -- $TLC[i]


Or (to print only till the first empty element):

for ((i = 1; $#TLC[i]; i++)) print -ru2 -- "$TLC[i]"

Or:

print -rlu2 -- "${(@)TLC[1,TLC[(i)]-1]}"

-- 
Stephane



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