Re: Returning a local array
- To: mathgroup at smc.vnet.net
- Subject: [mg84200] Re: Returning a local array
- From: dh <dh at metrohm.ch>
- Date: Thu, 13 Dec 2007 06:00:25 -0500 (EST)
- References: <fjqj9l$6l9$1@smc.vnet.net>
Hi Art,
you could e.g. define index/value pairs by h. Subsequently you could set
the values of g like:
h={{"a",1},{"b",2}};
(g [#[[1]]]=#[[2]])& /@ h;
this will set g["a"] to 1 e.t.c.
hope this helps, Daniel
Art wrote:
> I would like to return a local array. The below seems like the wrong
> way to do it:
>
> f[] := Module[{h}, h["a"] = 1; h["b"] = 2; h];
> g = f[];
>
> I would like g not to be associated with the local variable h, but
> equivalent to defining it as follows:
>
> g["a"] = 1; g["b"] = 2;
>
> Is it better to work with rules instead? It can become awkward and I
> would like to avoid it.
>
> f[] := Module[{h}, h={"a" -> 1, "b" -> 2}; h];
> g = f[];
> "a" /. g
>
> The specific problem is I am reading the header for a binary file that
> has 20 or so parameters describing the binary file. I thought this
> would be the analog of a structure in other languages and a good way
> to put these parameters in a variable.
>
> Thanks.
>