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

Re: Problem with associative arrays

On Dec 14, 11:55am, Sven Wischnowsky wrote:
} Subject: Problem with associative arrays
} Subscripts for associative arrays are matheval()ed

No, they are not.  The code in params.c looks like

	if (PM_TYPE(v->pm->flags) == PM_HASHED) {
	} else
	if (!(r = mathevalarg(s, &s)) || (isset(KSHARRAYS) && r >= 0))

} which makes some problems:
} % set -A a

You have to use `typeset -A a` to get an associative array.  `set -A a`
creates an ordinary array.  I realize this is confusing, which is why I
originally used a different option letter for typeset, but compatibility
with ksh requires -A.

} % a[foo]=bar
} % echo $a[baz]
} bar
} There is no variable baz, so evaluating it yields zero and since
} numeric subscripts on associative arrays work, this yields the `first' 
} value.

This is all accurate, for ordinary arrays.

Bart Schaefer                                 Brass Lantern Enterprises
http://www.well.com/user/barts              http://www.brasslantern.com

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