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

Re: for loop question



On Nov 4,  9:56am, Han Pingtian wrote:
}
} > for ((i=1; (z[$([ -n "$TLC[i]" ])0]),$? == 0; i++))
} >   print -ru2 -- $TLC[i]
} > 
} What does the (z[$([ -n "$TLC[i]" ])0]) mean, please?

It's an overly-obfuscated way to write $([ -n "$TLC[i]" ]).  The real
work is the ",$?" part.

z[...],$? is used to throw away the z[...] part and keep the value of $?.

The value of $? comes from the side-effect of the $(...) inside the
subscript of z[...].

The 0 is appended because [ -n "$TLC[i]" ] does not produce any output,
but there has to be a valid number as the subscript, hence z[0].

This could be written a LOT more plainly as

    for ((i=1; $([ -n "$TLC[i]" ]; echo $?) == 0; i++))

without needing any more characters.  If we're playing golf,

    for ((i=1; ! $([ "$TLC[i]" ]; echo $?); i++))

works too.

Which, by the way, points out another reason you can't just use the
exit status of a command in a math context:  command success/failure
are the reverse of true/false when taken as integer values.



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