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

Re: Typeset with array



On Jun 24, 10:14am, Peter Stephenson wrote:
}
} > This has me trying to think of ways to implement the ksh ([key]=value)
} > syntax.
} > 
} >     typeset -a varname=([k1]=v1 [k2]=v2)

Incidentally, I just stumbled over the bash feature that you can set
elements of a regular array this way as well.

bash$ array=([0]="a" [2]="b" [1]="")
bash$ typeset -p array
declare -a array='([0]="a" [1]="" [2]="b")'
bash$ 

I believe someone else already pointed out that looking inside the quoted
value for a parenthesized list has its own set of problems.

Ksh always creates an associative array with that syntax unless explictly
told otherwise, but it accepts it for normal arrays:

ksh$ array=([0]="a" [2]="b" [1]="")
ksh$ typeset -p array
typeset -A array=([0]=a [1]='' [2]=b)
ksh$ typeset -a array=([0]="a" [2]="b" [1]="")
ksh$ typeset -p array
typeset -a array=(a '' b)
ksh$ 

So it really is just the same as mapping the variable name across the
values in the list.

} However, You could get away with detecting the form until the list is

"not detecting"?

} expanded: that's now in two different places, addvars and execcmd, but
} could easily be made common --- put something in front of the
} ecgetlist() that retrieves the array.  As long as you detect it before
} attempting to glob, to avoid NO_MATCH behaviour, it ought to work.  This
} is easier as there are no wordcode changes and I can't see any obvious
} gotchas.  I'm not sure what expansions apply: do k* get expanded at all?
} Presumably v* get single word expansion?

ksh$ echo D*
Doc
ksh$ typeset array=([D*]=D*)
ksh$ typeset -p array
typeset -A array=(['D*']='D*')
ksh$ 

But note:

ksh$ typeset -A array=([*]=*)
ksh: *: invalid subscript in assignment
ksh$ 



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