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

Re: for loop question



On 11/02/2014 01:37 PM, Stephane Chazelas wrote:
You could with:

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

(not that you would want to).
No.  That is pure sadism ;-) ;-)

But it does show that 'for ((' CAN stop and digest ' [ -n "$TLC[i]" ] ' if it wants too, it just has to make it obscenely difficult. Why can't the truth test of a command just be taken
as 'arithmetic' plain and simple?

     $ for ((; 1 ;)) echo true!
     true!
     true!
     true!
     .....

     $ for ((; 0 ;)) echo true!
[nothing]

.... so why/how is it that the return value of a ' [] ' test is NOT either 1 or 0?


Thanks for these, there is much  to learn from them:
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]}"




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