* Problem with associative arrays
@ 1998-12-16 14:31 Sven Wischnowsky
1998-12-16 17:53 ` PATCH: 3.1.5-pws-3: (subscripts) " Bart Schaefer
0 siblings, 1 reply; 5+ 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] 5+ messages in thread
* PATCH: 3.1.5-pws-3: (subscripts) Re: Problem with associative arrays
1998-12-16 14:31 Problem with associative arrays Sven Wischnowsky
@ 1998-12-16 17:53 ` Bart Schaefer
0 siblings, 0 replies; 5+ messages in thread
From: Bart Schaefer @ 1998-12-16 17:53 UTC (permalink / raw)
To: zsh-workers
On Dec 16, 3:31pm, Sven Wischnowsky wrote:
} Subject: Problem with associative arrays
}
} 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).
isident() seems to be rather inconsistent about strict checking of the
text inside the [ ]. Below is the most obvious fix (#ifdef'd for now).
This has the side-effect of making associative array syntax more like
ksh, in that you can put quoted strings inside the subscript:
zsh% typeset -A foo
zsh% foo['a b c']=cba
zsh% print -l ${(kv)foo}
a b c
cba
However, this fails (because of the "balanced brackets" test):
zsh% foo['a ] c']=cba
zsh: not an identifier: foo[a ] c]
So a better test might be just strchr(ss, ']') and let other code report
subscripting errors after it knows what kind of array it's accessing. I
also don't know how widely available strcspn() is; this might break the
build on some older platforms.
Also, this is a bit strange:
zsh% echo $foo['a b c']
zsh% echo ${foo[a b c]} <-- note, must omit the quotes on 'a b c'
cba
Probably the subscript needs to be untokenized somewhere.
Index: Src/params.c
===================================================================
--- params.c 1998/12/15 06:08:15 1.16
+++ params.c 1998/12/16 17:19:05
@@ -633,6 +633,7 @@
if (!iident(*ss))
break;
+#if 0
/* If this exhaust `s' or the next two characters *
* are [(, then it is a valid identifier. */
if (!*ss || (*ss == '[' && ss[1] == '('))
@@ -642,6 +643,7 @@
* definitely not a valid identifier. */
if (*ss != '[')
return 0;
+
noeval = 1;
(void)mathevalarg(++ss, &ss);
if (*ss == ',')
@@ -650,6 +652,19 @@
if (*ss != ']' || ss[1])
return 0;
return 1;
+#else
+ /* If the next character is not [, then it is *
+ * definitely not a valid identifier. */
+ if (!*ss)
+ return 1;
+ if (*ss != '[')
+ return 0;
+
+ /* Require balanced [ ] pairs */
+ for (s = ss; *(s += strcspn(++ss, "[]")) && s > ss; ss = s)
+ ;
+ return (*ss == ']' && !s[1]);
+#endif
}
static char **garr;
--
Bart Schaefer Brass Lantern Enterprises
http://www.well.com/user/barts http://www.brasslantern.com
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: Problem with associative arrays
@ 1998-12-15 8:12 Sven Wischnowsky
0 siblings, 0 replies; 5+ 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] 5+ messages in thread
* Problem with associative arrays
@ 1998-12-14 10:55 Sven Wischnowsky
1998-12-14 15:47 ` Bart Schaefer
0 siblings, 1 reply; 5+ 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] 5+ messages in thread
* Re: Problem with associative arrays
1998-12-14 10:55 Sven Wischnowsky
@ 1998-12-14 15:47 ` Bart Schaefer
0 siblings, 0 replies; 5+ 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] 5+ messages in thread
end of thread, other threads:[~1998-12-16 18:06 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
1998-12-16 14:31 Problem with associative arrays Sven Wischnowsky
1998-12-16 17:53 ` PATCH: 3.1.5-pws-3: (subscripts) " Bart Schaefer
-- strict thread matches above, loose matches on Subject: below --
1998-12-15 8:12 Sven Wischnowsky
1998-12-14 10:55 Sven Wischnowsky
1998-12-14 15:47 ` Bart Schaefer
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).