Re: converting a string to an IDL command [message #62381 is a reply to message #62377] |
Mon, 08 September 2008 12:25   |
Michael Galloy
Messages: 1114 Registered: April 2006
|
Senior Member |
|
|
On Sep 8, 1:13 pm, Mark <astrobo...@gmail.com> wrote:
> Actually, I have a related question, for which if it's obvious I
> apologize in advance.
>
> Not sure how to put this precisely, but....
>
> Let's say I have an array called "happydays".
>
> I'd like to make a function that would take the string 'happydays',
> and say, return the min and max of the array with that name:
>
> out=some_function('happydays')
> where out is the string:
>
> 'The max and min of HAPPYDAYS is 0 and 15"
How about:
pro mg_printvar, name
compile_opt strictarr
var = scope_varfetch(name, level=-1L)
varMin = min(var, max=varMax)
print, 'The range of ' + strupcase(name) + ' is ' + strtrim(varMin,
2) + ' to ' + strtrim(varMax, 2)
end
This works like:
IDL> a = randomu(seed, 10)
IDL> mg_printvar, 'a'
The range of A is 0.0932334 to 0.960451
> Just as well, would be something like:
>
> out=some_function(happydays)
> and out is the same string as above.
>
> In other words, I guess what I'm asking is, is there a way to find
> the original name of a variable that was passed to a sub-program or a
> function....
This one uses SCOPE_VARNAME to do the dirty work:
pro mg_printvar2, var
compile_opt strictarr
name = scope_varname(var, level=-1L)
varMin = min(var, max=varMax)
print, 'The range of ' + strupcase(name) + ' is ' + strtrim(varMin,
2) + ' to ' + strtrim(varMax, 2)
end
For example:
IDL> mg_printvar2, a
The range of A is 0.0932334 to 0.960451
Mike
--
www.michaelgalloy.com
Tech-X Corporation
Software Developer II
|
|
|