MathGroup Archive 2010

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

Search the Archive

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


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