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

Re: Typeset with array



On Thu, 25 Jun 2015 08:16:53 -0700
> Some quick tests:
> 
> torch% typeset var var[2]=two 
> torch% typeset -p var
> typeset var=two

That's correct: var is created then manipulated as a scalar.  Apart from
localness this is the same effect as

var=
var[2]=two

> torch% typeset -a array array[2]=two
> typeset: array[2]: inconsistent type for assignment
> torch% typeset -p array
> typeset -a array
> array=()

That's also correct.  typeset -a creates arrays, so "array" is
successfully created; however, array[2] is a scalar with a scalar
assignment, so that's an error with the "-a".  (However, if you think
you can work out how to relax the test to allow array elements without
the whole thing blowing up in your face, be my guest...)

> torch% typeset newarray=() newarray[2]=two 

That's the right way to mix arrays and scalars...

> zsh: segmentation fault (core dumped)  Src/zsh -f

..so, as it happens, that's not correct.  That reminds me of something
else I meant to test...

diff --git a/Src/exec.c b/Src/exec.c
index 57e8f63..50a11eb 100644
--- a/Src/exec.c
+++ b/Src/exec.c
@@ -3583,15 +3583,18 @@ execcmd(Estate state, int input, int output, int how, int last1)
 			    asg->value.array =
 				ecgetlist(state, WC_ASSIGN_NUM(ac),
 					  EC_DUPTOK, &htok);
-			    prefork(asg->value.array, PREFORK_ASSIGN);
-			    if (errflag) {
-				state->pc = opc;
-				break;
-			    }
-			    globlist(asg->value.array, 0);
-			    if (errflag) {
-				state->pc = opc;
-				break;
+			    if (asg->value.array)
+			    {
+				prefork(asg->value.array, PREFORK_ASSIGN);
+				if (errflag) {
+				    state->pc = opc;
+				    break;
+				}
+				globlist(asg->value.array, 0);
+				if (errflag) {
+				    state->pc = opc;
+				    break;
+				}
 			    }
 			}
 
diff --git a/Test/B02typeset.ztst b/Test/B02typeset.ztst
index e6285bc..1548b81 100644
--- a/Test/B02typeset.ztst
+++ b/Test/B02typeset.ztst
@@ -663,3 +663,16 @@
 >fn2 () {
 >  typeset assignfirst=(why not) 
 >}
+
+  fn() {
+    typeset array=()
+    print ${(t)array} ${#array}
+    typeset gnothergarray=() gnothergarray[1]=yes gnothergarray[2]=no
+    print -l ${(t)gnothergarray} $gnothergarray
+  }
+  fn
+0:can set empty array
+>array-local 0
+>array-local
+>yes
+>no



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