MathGroup Archive 2010

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: something nice I found today, return multiple values from a function

  • To: mathgroup at smc.vnet.net
  • Subject: [mg113130] Re: something nice I found today, return multiple values from a function
  • From: "Nasser M. Abbasi" <nma at 12000.org>
  • Date: Wed, 13 Oct 2010 23:28:35 -0400 (EDT)
  • References: <i927an$3io$1@smc.vnet.net> <i93kbo$e67$1@smc.vnet.net>
  • Reply-to: nma at 12000.org

I thought I should also show how this method works when used in Tables.

Given this:

computeSomething[r_] := Module[{var1 = -99, var2 = 20, result},
    result["x"] = var1*r;
    result["y"] = var2*r;
    result
];

And now call it many times:

tbl = Table[computeSomething[30], {i, 5}];

Now to access each result, it will be like this:

tbl[[1]]["x"]  ---> in place of the old way: tbl[[1,1]]
tbl[[1]]["y"]  ---> in place of the old way: tbl[[1,2]]

tbl[[2]]["x"]  ---> in place of the old way: tbl[[2,1]]

or

For[i=1,i<5,i++,Print[tbl[[i]]["x"]]]
-2970
-2970
-2970
-2970

etc...


Should I patent this method? :)

--Nasser


  • Prev by Date: Re: discrete numerical 2D gradient
  • Next by Date: Re: Simplifying Logs
  • Previous by thread: Re: something nice I found today, return multiple values from a function
  • Next by thread: Re: something nice I found today, return multiple values from a function