zsh-workers
 help / color / mirror / code / Atom feed
* Problem with associative arrays
@ 1998-12-14 10:55 Sven Wischnowsky
  1998-12-14 15:47 ` Bart Schaefer
  0 siblings, 1 reply; 4+ messages in thread
From: Sven Wischnowsky @ 1998-12-14 10:55 UTC (permalink / raw)
  To: zsh-workers


Hello

Subscripts for associative arrays are matheval()ed which makes some
problems:

% set -A 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.

% a[x*]=frob
zsh: bad math expression: unbalanced stack
zsh: bad math expression: unbalanced stack

Yes, `x*' is an invalid math expression, but...

Bye
 Sven


--
Sven Wischnowsky                         wischnow@informatik.hu-berlin.de


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Problem with associative arrays
  1998-12-14 10:55 Problem with associative arrays Sven Wischnowsky
@ 1998-12-14 15:47 ` Bart Schaefer
  0 siblings, 0 replies; 4+ messages in thread
From: Bart Schaefer @ 1998-12-14 15:47 UTC (permalink / raw)
  To: Sven Wischnowsky, zsh-workers

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


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Problem with associative arrays
@ 1998-12-16 14:31 Sven Wischnowsky
  0 siblings, 0 replies; 4+ messages in thread
From: Sven Wischnowsky @ 1998-12-16 14:31 UTC (permalink / raw)
  To: zsh-workers


And this time it's for real:

  % typeset -A a
  % a[x*]=foo
  zsh: bad math expression: unbalanced stack
  % echo $a[x*]
  foo
  %

The problem is that isident() uses mathevalarg() to skip (!) over the
contents of a subscript (unless the subscript contains flags).

I don't have the time to fix this now since our LAN will soon be
upgraded but if noone else gives it a try I'll have a lokk at it.

Bye
 Sven


--
Sven Wischnowsky                         wischnow@informatik.hu-berlin.de


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Problem with associative arrays
@ 1998-12-15  8:12 Sven Wischnowsky
  0 siblings, 0 replies; 4+ messages in thread
From: Sven Wischnowsky @ 1998-12-15  8:12 UTC (permalink / raw)
  To: zsh-workers


Bart Schaefer wrote:

> ...
> 
> 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.

Oops. Sorry.

Bye
 Sven


--
Sven Wischnowsky                         wischnow@informatik.hu-berlin.de


^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~1998-12-16 14:44 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
1998-12-14 10:55 Problem with associative arrays Sven Wischnowsky
1998-12-14 15:47 ` Bart Schaefer
1998-12-15  8:12 Sven Wischnowsky
1998-12-16 14:31 Sven Wischnowsky

Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).