[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Modifying global variables from within functions
From: |
Mirek Kwasniak |
Subject: |
Re: Modifying global variables from within functions |
Date: |
Wed, 14 Nov 2001 21:26:43 +0100 |
User-agent: |
Mutt/1.3.23i |
On Tue, Nov 13, 2001 at 05:26:28PM -0300, Roberto Hernandez wrote:
[...]
> function foo(varName, newValue)
> eval(["global ", varName]);
>
> if isnumeric(newValue)
> eval([varName, " = " , num2str(newValue), ";"])
> else
> eval([varName, " = \"" , newValue, "\";"])
> endif
> endfunction
[...]
> But if I want to do more complex
> operations inside the function "foo", then having to use "eval" and
> building lots of strings becomes annoying. It's easy to make mistakes
> and pretty bad for debugging.
>
> The question is: Is there a better way to do this?
When you don't care about side-effects I can offer some ideas:
function foo(varName, newValue)
eval([ "global ", varName ]);
eval([ "tmpvar=", varName ';' ]);
do_what_you_want(tmpvar)
eval([ "tmp=", varName '=tmpvar;' ]);
endfunction
or even simpler:
function wrapper(varName, newValue)
eval([ 'global ', varName ]);
eval([ varName, ' = foo(', varName, ', newValue);' ]);
endfunction
Mirek
-------------------------------------------------------------
Octave is freely available under the terms of the GNU GPL.
Octave's home on the web: http://www.octave.org
How to fund new projects: http://www.octave.org/funding.html
Subscription information: http://www.octave.org/archive.html
-------------------------------------------------------------