Discussion:
[ast-developers] [ast-users] ksh93 returns "()" for "${a[@]}" if I clear an array with a=( )
Roland Mainz
2013-07-11 15:14:09 UTC
Permalink
--------
(Slightly) offtopic: How can I clear a plain string array (e.g. $
typeset -a ar #) via the C API (without having to resort to
|sh_trap()| to do it...) ?
You can do
nv_putsub(np,0,0,ARRAY_UNDEF);
before calling
_nv_unset(np,NV_RDONLY);
Ok... so a function to clear an associative/indexed array would look like this:
-- snip --
void nv_clear_array(Namval_t* np)
{
nv_putsub(np,0,0,ARRAY_UNDEF);
_nv_unset(np,NV_RDONLY);
}
-- snip --
... right ?

Next question: How do I clear a compound variable using the libshell C API ?

----

Bye,
Roland
--
__ . . __
(o.\ \/ /.o) roland.mainz at nrubsig.org
\__\/\/__/ MPEG specialist, C&&JAVA&&Sun&&Unix programmer
/O /==\ O\ TEL +49 641 3992797
(;O/ \/ \O;)
David Korn
2013-07-11 20:58:50 UTC
Permalink
cc: ast-developers at research.att.com
Subject: Re: Re: Re: [ast-users] ksh93 returns "()" for "${a[@]}" if I clear an array with a=( )
--------
Post by Roland Mainz
(Slightly) offtopic: How can I clear a plain string array (e.g. $
typeset -a ar #) via the C API (without having to resort to
|sh_trap()| to do it...) ?
You can do
nv_putsub(np,0,0,ARRAY_UNDEF);
before calling
_nv_unset(np,NV_RDONLY);
-- snip --
void nv_clear_array(Namval_t* np)
{
nv_putsub(np,0,0,ARRAY_UNDEF);
_nv_unset(np,NV_RDONLY);
}
-- snip --
... right ?
Next question: How do I clear a compound variable using the libshell C API ?
Use
_nv_unset(np,NV_RDONLY);
just as you do for a simple variable.

By the way, use 0 instead of NV_RDONLY if you don't want to remove a readonly
variable.

David Korn
dgk at research.att.com
Cedric Blancher
2013-07-12 18:29:32 UTC
Permalink
cc: ast-users at research.att.com wendlin1974 at gmail.com
--------
cc: ast-users at research.att.com
--------
a=()
doesn't clear an array, it unsets a and creates an empty compound variable
(
)
typeset -a a=()
should clear an array.
This is not very intuitive. First variable a has already be declared
as an array so the intuitive feeling would be that a=() resets it with
an empty array. 2nd your interpretation assumes that the called
function knows the array data type to reset it - which becomes a pain
if you have hundreds of types defined and try to use a utility
function which should reset an array of any of these types.
I'd better go for: a=() means reset the variable if variable a is
either array or compound. If it isn't already a type of these make it
a compound variable. If variable a is of type integer or string throw
an error.
This should make things more predictable.
Ced
--
Cedric Blancher <cedric.blancher at googlemail.com>
Institute Pasteur
I changed this for the next alpha so that
a=()
will preserve index and associative array attributes while unsetting the array.
However, it is not an error to redefine a simple variable as a compound variable.
OK, lets try that out.
Can we have the patch for testing, please?
Trying again to post the mail to the ast lists

Ced
--
Cedric Blancher <cedric.blancher at gmail.com>
Institute Pasteur
David Korn
2013-07-15 00:59:00 UTC
Permalink
cc: wendlin1974 at gmail.com ast-developers at research.att.com
Subject: Re: Re: [ast-users] ksh93 returns "()" for "${a[@]}" if I clear an array with a=( )
--------

Here is the patch:

--- old/sh/name.c Mon Jun 24 15:37:10 2013
+++ new/sh/name.c Thu Jul 11 13:50:50 2013
@@ -455,7 +457,14 @@
if(tp->tre.tretyp==0 && !tp->com.comset && !tp->com.comarg)
{
if(!(arg->argflag&ARG_APPEND) && nv_isattr(np,NV_BINARY|NV_NOFREE|NV_RAW)!=(NV_BINARY|NV_NOFREE|NV_RAW))
+ {
+ array = nv_isarray(np);
_nv_unset(np,NV_EXPORT);
+ if(ap && ap->fun)
+ nv_setarray(np,nv_associative);
+ else if(ap || array)
+ nv_onattr(np,NV_ARRAY);
+ }
goto skip;
}
if(tp->tre.tretyp==TLST || !tp->com.comset || tp->com.comset->argval[0]!='[')

David Korn
dgk at research.att.com
Roland Mainz
2013-07-17 03:20:03 UTC
Permalink
Post by David Korn
cc: wendlin1974 at gmail.com ast-developers at research.att.com
--------
--- old/sh/name.c Mon Jun 24 15:37:10 2013
+++ new/sh/name.c Thu Jul 11 13:50:50 2013
@@ -455,7 +457,14 @@
if(tp->tre.tretyp==0 && !tp->com.comset && !tp->com.comarg)
{
if(!(arg->argflag&ARG_APPEND) && nv_isattr(np,NV_BINARY|NV_NOFREE|NV_RAW)!=(NV_BINARY|NV_NOFREE|NV_RAW))
+ {
+ array = nv_isarray(np);
_nv_unset(np,NV_EXPORT);
+ if(ap && ap->fun)
+ nv_setarray(np,nv_associative);
+ else if(ap || array)
+ nv_onattr(np,NV_ARRAY);
+ }
goto skip;
}
if(tp->tre.tretyp==TLST || !tp->com.comset || tp->com.comset->argval[0]!='[')
Erm... the patch doesn't seem to work for arrays of user-defined types:
-- snip --
$ ksh -c 'typeset -T x_t=( integer i=1 ) ; compound c=( x_t -a ar=(
(i=2) (i=3) ) ); c.ar=( ) ; print -v c'
(
x_t -a ar=(
[0]=(
typeset -l -i i=2
)
[1]=(
typeset -l -i i=3
)
)
)
-- snip --
... AFAIK it should print:
-- snip --
(
x_t -a ar
)
-- snip --

Same applies to 4D indexed arrays of user-defined types...

----

Bye,
Roland
--
__ . . __
(o.\ \/ /.o) roland.mainz at nrubsig.org
\__\/\/__/ MPEG specialist, C&&JAVA&&Sun&&Unix programmer
/O /==\ O\ TEL +49 641 3992797
(;O/ \/ \O;)
Roland Mainz
2013-07-17 03:30:58 UTC
Permalink
Post by Roland Mainz
Post by David Korn
cc: wendlin1974 at gmail.com ast-developers at research.att.com
--------
--- old/sh/name.c Mon Jun 24 15:37:10 2013
+++ new/sh/name.c Thu Jul 11 13:50:50 2013
@@ -455,7 +457,14 @@
if(tp->tre.tretyp==0 && !tp->com.comset && !tp->com.comarg)
{
if(!(arg->argflag&ARG_APPEND) && nv_isattr(np,NV_BINARY|NV_NOFREE|NV_RAW)!=(NV_BINARY|NV_NOFREE|NV_RAW))
+ {
+ array = nv_isarray(np);
_nv_unset(np,NV_EXPORT);
+ if(ap && ap->fun)
+ nv_setarray(np,nv_associative);
+ else if(ap || array)
+ nv_onattr(np,NV_ARRAY);
+ }
goto skip;
}
if(tp->tre.tretyp==TLST || !tp->com.comset || tp->com.comset->argval[0]!='[')
-- snip --
$ ksh -c 'typeset -T x_t=( integer i=1 ) ; compound c=( x_t -a ar=(
(i=2) (i=3) ) ); c.ar=( ) ; print -v c'
(
x_t -a ar=(
[0]=(
typeset -l -i i=2
)
[1]=(
typeset -l -i i=3
)
)
)
-- snip --
-- snip --
(
x_t -a ar
)
-- snip --
Same applies to 4D indexed arrays of user-defined types...
Erm... even the plain compound array case doesn't work:
-- snip --
$ ksh -c 'compound c=( compound -A ar=( [0]=(i=2) [1]=(i=3) ) );
c.ar=( ) ; print -v c'
(
typeset -A ar
)
-- snip --
... it should print:
-- snip --
(
typeset -C -A ar
)
-- snip --

----

Bye,
Roland
--
__ . . __
(o.\ \/ /.o) roland.mainz at nrubsig.org
\__\/\/__/ MPEG specialist, C&&JAVA&&Sun&&Unix programmer
/O /==\ O\ TEL +49 641 3992797
(;O/ \/ \O;)
Loading...