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

Re: alias with a parameter



2021-04-15 20:30:46 +0000, Daniel Shahaf:
> Bart Schaefer wrote on Thu, Apr 15, 2021 at 11:19:16 -0700:
> > On Wed, Apr 14, 2021 at 4:10 AM zzapper <zsh@xxxxxxxxxxxxxx> wrote:
> > >    if [ $# -gt 0 ] ; then
> > 
> > It's generally preferable to use [[ ... ]] instead of [ ... ] because
> > "[" is just another name for "test" and "]" is just one of its
> > arguments, whereas "[[" and "]]" are syntax tokens with parsing rules
> > for what appears between them.  However, since you're comparing
> > numbers, you could use (( $# > 0 )) here.
> 
> In general, «(( ${+foo[1]} ))» is faster than «(( ${#foo} ))», because
> the latter takes O(N) time where N is the length of the array, where's
> the former is O(1).


Beware that (( ${+foo[1]} )) approach doesn't work when the
ksharrays option is enabled. (though I'd expect one would
generally only use that option as part of the ksh emulation to
interpret ksh code, so that would likely not be a problem).

It also does't work for associative arrays.

FWIW, [[ -v foo[1] ]] seems to be slightly faster.

I'd still use (( $#foo )) or (( $#foo > 0 )) for legibility.

-- 
Stephane




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