David Korn wrote:
> Subject: Re: [ast-users] Seeking C code example for "${!myli...@]}"
> --------
> > Are there any examples which show how I can use libshell to access all
> > members of an indexed or associative array? For example I'd like to
> > create a shell via sh_init, use sh_trap to run shell code and then use
> > C code to get the index names of all elements in array 'mylist', i.e.
> > a C version of "${!myli...@]}"
> > Does anyone have a related code example?
>
> ===============cut here========================
> #include <nval.h>
>
> int outname(Sfio_t *out, Namval_t* np)
> {
> int i, n=0, assoc=(nv_aindex(np)<0);
> nv_putsub(np, (char*)0, ARRAY_SCAN);
> do
> {
> if(assoc)
> sfputr(out, nv_getsub(np),'\n');
> else
> sfprintf(out, %d\n",i);
> n++;
> }
> while(nv_nextsub(np));
> return(n);
> }
> ===============cut here========================
Wouldn't it be easier to use |nv_opensub(np)| to get a reference to the
first array element and the cyle through all elements:
-- snip --
/* open np to array */
np = nv_open("x", shp->var_tree, 0);
/* cycle through all array elements */
for( np_sub = nv_opensub(np) ; nv_nextsub(np_sub) != 0 ; )
{
}
-- snip --
----
Bye,
Roland
--
__ . . __
(o.\ \/ /.o) [email protected]
\__\/\/__/ MPEG specialist, C&&JAVA&&Sun&&Unix programmer
/O /==\ O\ TEL +49 641 3992797
(;O/ \/ \O;)
_______________________________________________
ast-users mailing list
[email protected]
https://mailman.research.att.com/mailman/listinfo/ast-users