Wendy Lin wrote:
> 
> 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?

Please try the code below - it should work for both indexed and
associative arrays:
-- snip --
#include <shell.h>
#include <nval.h>
#include <stdio.h>

int main(int argc, char *argv[])
{
       Namval_t *np;
       Namval_t *np_sub;
       Sfdouble_t d;

       Shell_t *shp = sh_init(argc, argv, 0);

       //sh_trap("typeset -a x=(1 2)",0);
       sh_trap("typeset -A x=([a]=1 [b]=2)",0);

       np = nv_open("x", shp->var_tree, 0);
       
       printf("nv_isarray(np)=%d\n", nv_isarray(np));
       nv_putsub(np, NULL, ARRAY_SCAN);
       np_sub = np;

       do
       {
                printf("%lx: sub='%s'\n", (long)np_sub, nv_getsub(np_sub));
       } while (np_sub && nv_nextsub(np_sub));

       nv_close(np);

       return(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

Reply via email to