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

PATCH: array slice



On Thu, 25 Jun 2015 10:29:23 +0100
Peter Stephenson <p.stephenson@xxxxxxxxxxx> wrote:
> > That could also be extended to something like var[2,7]=(1 2)
> 
> Extending assignments (at least to existing arrays) to handle array
> slices would certainly be natural, yes.  I hope m[o]st of the mechanism is
> already present and just needs borrowing from addvars().

It looks like half closing your eyes, sticking your fingers in your
ears, and copying what's there already does the trick.

Nice to keep procedures consistent.

(By the way, if I've been following what we've been doing,

  typeset foo[3]=blah

is now guaranteed not to give you a "no match" error or bogus glob
match, though

  typeset foo[3]

isn't, but that doesn't matter as you can't change the attributes of an
array element on its own.)

pws

diff --git a/Src/builtin.c b/Src/builtin.c
index bc68545..3da1678 100644
--- a/Src/builtin.c
+++ b/Src/builtin.c
@@ -2347,9 +2347,16 @@ typeset_single(char *cname, char *pname, Param pm, UNUSED(int func),
 	    asg->is_array = 0;
 	    keeplocal = 0;
 	    on = pm->node.flags;
+	} else if (PM_TYPE(on) == PM_ARRAY && ASG_ARRAYP(asg)) {
+	    if (!(pm = setaparam(pname, asg->value.array ? zlinklist2array(asg->value.array) :
+				 mkarray(NULL))))
+		return NULL;
+	    asg->value.array = NULL;
+	    keeplocal = 0;
+	    on = pm->node.flags;
 	} else {
 	    zerrnam(cname,
-		    "%s: array elements must be scalar", pname);
+		    "%s: inconsistent array element or slice assignment", pname);
 	    return NULL;
 	}
     }
diff --git a/Test/B02typeset.ztst b/Test/B02typeset.ztst
index 1548b81..5d69e5d 100644
--- a/Test/B02typeset.ztst
+++ b/Test/B02typeset.ztst
@@ -676,3 +676,18 @@
 >array-local
 >yes
 >no
+
+  array=(nothing to see here)
+  fn() {
+    typeset array=(one two three four five)
+    typeset array[2,4]=(umm er)
+    print ${#array} $array
+    typeset array[2,3]=()
+    print ${#array} $array
+  }
+  fn
+  print ${#array} $array
+0:can update array slices in typeset
+>4 one umm er five
+>2 one five
+>4 nothing to see here



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