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

Re: for loop question



Ray Andrews 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?

Inside of (( ... )) is arithmetic context. So different syntax is valid
there. [ is just a command. Look at the error message you get if you put
it there:
  % for ((; [ -f foo ] ; )) echo true
  zsh: bad output format specification

Zsh sees the square brackets and in arithmetic context, square brackets
are used for selecting an output format, specifically a number base. For
example:
  % echo $(( [#2] 37 ))
  2#100101

There's also a command named true which just returns true. So what do
you think the following might do:

  % for ((; true ;)) echo true!

This actually looks for the variable $true and interprets it as a
number.

As you can see, the syntaxes conflict. While we could add a simple way
to escape back into normal syntax from arithmetic syntax, it'd be no
nicer than the existing alternatives.

Oliver



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