zsh-workers
 help / color / mirror / code / Atom feed
* PATCH: subscript flags on lhs of assignment
@ 2004-03-31 13:20 Oliver Kiddle
  2004-03-31 15:58 ` Bart Schaefer
  0 siblings, 1 reply; 3+ messages in thread
From: Oliver Kiddle @ 2004-03-31 13:20 UTC (permalink / raw)
  To: Zsh workers

This patch allows assignments to associative array elements where the
(i) or (r) subscript flag is used to select the element. This means you
can do:
  typeset -A assoc
  assoc=(one a two b)
  assoc[(r)a]=new
At the moment, this prints the message:
  "attempt to set slice of associative array"
but that is only really applicable for the (I), (K) and (R) subscript
flags. The SCANPM_MATCHMANY flag also seems to get set for the (k)
flag. Is that a bug?

There may well be better ways to implement this. Using the (k), (i) and
(r) flags seems to result in one element arrays instead of single
values so it would be better if fetchvalue was made to return with
v->pm pointing to the actual association element instead. That couldn't
easily maintain compatibility with (i) outputting keys instead of
values though. This seems to work and is fairly minimal in terms of the
number of changes.

Oliver

Index: Src/params.c
===================================================================
RCS file: /cvsroot/zsh/zsh/Src/params.c,v
retrieving revision 1.80
diff -u -r1.80 params.c
--- Src/params.c	30 Mar 2004 16:35:38 -0000	1.80
+++ Src/params.c	31 Mar 2004 08:59:17 -0000
@@ -381,6 +381,7 @@
 static Patprog scanprog;
 static char *scanstr;
 static char **paramvals;
+static Param foundparam;     
 
 /**/
 void
@@ -404,6 +405,7 @@
     } else if ((flags & SCANPM_MATCHKEY) && !pattry(scanprog, v.pm->nam)) {
 	return;
     }
+    foundparam = v.pm;
     if (flags & SCANPM_WANTKEYS) {
 	paramvals[numparamvals++] = v.pm->nam;
 	if (!(flags & (SCANPM_WANTVALS|SCANPM_MATCHVAL)))
@@ -1598,7 +1600,7 @@
 	zsfree(val);
 	return;
     }
-    if (v->pm->flags & PM_HASHED) {
+    if ((v->pm->flags & PM_HASHED) && (v->isarr & SCANPM_MATCHMANY)) {
 	zerr("%s: attempt to set slice of associative array", v->pm->nam, 0);
 	zsfree(val);
 	return;
@@ -1663,6 +1665,11 @@
 	    setarrvalue(v, ss);
 	}
 	break;
+    case PM_HASHED:
+        {
+	    (foundparam->sets.cfn) (foundparam, val);
+        }
+	break;
     }
     if ((!v->pm->env && !(v->pm->flags & PM_EXPORTED) &&
 	 !(isset(ALLEXPORT) && !(v->pm->flags & PM_HASHELEM))) ||


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

* Re: PATCH: subscript flags on lhs of assignment
  2004-03-31 13:20 PATCH: subscript flags on lhs of assignment Oliver Kiddle
@ 2004-03-31 15:58 ` Bart Schaefer
  2004-04-06 12:58   ` Oliver Kiddle
  0 siblings, 1 reply; 3+ messages in thread
From: Bart Schaefer @ 2004-03-31 15:58 UTC (permalink / raw)
  To: Zsh workers

On Mar 31,  3:20pm, Oliver Kiddle wrote:
}
} The SCANPM_MATCHMANY flag also seems to get set for the (k)
} flag. Is that a bug?

No, it's a hack to work around another shortcoming.  In order to use the
keys as the search patterns, I had to pretend to be interested in all
the keys, even though only one of them will eventually be used to return
a matching element.  Or something to that effect, it's been a very long
time since I wrote that code.

} There may well be better ways to implement this. Using the (k), (i) and
} (r) flags seems to result in one element arrays instead of single
} values

I believe that's because, for "normal" arrays, you can do slices with two
patterns e.g. $array[(r)left,(i)right].

It's because they return one-element arrays that assignments previously
didn't work for associatives.

} This seems to work and is fairly minimal

Nice.  You'll need to update the docs, of course; the INability to use
a pair of patterns for associative arrays should have been explicitly
mentioned before, and the stuff about assignments not working will have
to move from the "r" section to the "R" section.


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

* Re: PATCH: subscript flags on lhs of assignment
  2004-03-31 15:58 ` Bart Schaefer
@ 2004-04-06 12:58   ` Oliver Kiddle
  0 siblings, 0 replies; 3+ messages in thread
From: Oliver Kiddle @ 2004-04-06 12:58 UTC (permalink / raw)
  To: Zsh workers

On 31 Mar, Bart wrote:
> Nice.  You'll need to update the docs, of course; the INability to use
> a pair of patterns for associative arrays should have been explicitly
> mentioned before, and the stuff about assignments not working will have
> to move from the "r" section to the "R" section.

Okay, here's the doc change.

Oliver

Index: Doc/Zsh/params.yo
===================================================================
RCS file: /cvsroot/zsh/zsh/Doc/Zsh/params.yo,v
retrieving revision 1.20
diff -u -r1.20 params.yo
--- Doc/Zsh/params.yo	8 Mar 2004 16:57:41 -0000	1.20
+++ Doc/Zsh/params.yo	6 Apr 2004 12:41:33 -0000
@@ -198,14 +198,14 @@
 scalar and the `tt(w)' flag is given, respectively).  The subscript used
 is the number of the matching element, so that pairs of subscripts such as
 `tt($foo[(r))var(??)tt(,3])' and `tt($foo[(r))var(??)tt(,(r)f*])' are
-possible.  If the parameter is an associative array, only the value part
-of each pair is compared to the pattern, and the result is that value.
-Reverse subscripts may be used for assigning to ordinary array elements,
-but not for assigning to associative arrays.
+possible if the parameter is not an associative array.  If the
+parameter is an associative array, only the value part of each pair is
+compared to the pattern, and the result is that value.
 )
 item(tt(R))(
 Like `tt(r)', but gives the last match.  For associative arrays, gives
-all possible matches.
+all possible matches. May be used for assigning to ordinary array
+elements, but not for assigning to associative arrays.
 )
 item(tt(i))(
 Like `tt(r)', but gives the index of the match instead; this may not be
  


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

end of thread, other threads:[~2004-04-06 13:00 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2004-03-31 13:20 PATCH: subscript flags on lhs of assignment Oliver Kiddle
2004-03-31 15:58 ` Bart Schaefer
2004-04-06 12:58   ` Oliver Kiddle

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