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

Re: [PATCH] Optimization of getarrvalue()



On Nov 16, 11:06pm, Jun T. wrote:
}
} The document says (15.2.1)
} 
}    If  the  KSH_ARRAYS  option  is not set, then by default accesses to an
}    array element with a subscript that evaluates to zero return  an  empty
}    string,
} 
} so someone may expect that "${(@)a[0]}" is also replaced by an empty
} string "" rather than removed from the command line. But I'm not sure.

"Return an empty string" is not entirely accurate.  For example:

torch% setopt nounset
torch% a=()
torch% print $a[0]
zsh: a[0]: parameter not set

It would be more accurate to say "accesses to an array element with a
subscript that evaluates to zero behave in the same way as accesses to
any unset scalar parameter."

} And I fear changing this behavior might break some existing scripts.

The reason too-large subscripts behave differently than too-small is
that arrays can be extended to the right but not to the left.  That is,
if you assign to ary[bignum] you implicitly create empty elements for
any unoccupied indices less than bignum.  (This differs from e.g. bash
which uses a sparse implementation of arrays so the intervening elements
remain unset.)  A reference "off the right end" of the array behaves
consistently with a reference into such a "gap".

(This is also in part why I attempted to engage on NO_UNSET being too
aggressive with arrays, but nobody took up the discussion.)

Zero is special in that it's neither a "gap" nor an error (unless you try
to assign to it).



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