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

Re: for loop question



On Sun, Nov 2, 2014 at 10:37 PM, Stephane Chazelas
<stephane.chazelas@xxxxxxxxx> wrote:
> 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]}"

We might want to avoid using obscure mixes of the discouraged
alternate syntax with syntax that depends on short_loops being set,
when helping people who are asking questions about basic syntax. :)

-- 
Mikael Magnusson



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