Re: name a variable with number [message #87459 is a reply to message #87456] |
Tue, 04 February 2014 09:28   |
rjp23
Messages: 97 Registered: June 2010
|
Member |
|
|
On Tuesday, February 4, 2014 2:09:16 PM UTC, Fabien wrote:
> Hi,
>
>
>
>
>
> On 04.02.2014 14:57, David Fanning wrote:
>
>> I think in the hands of someone who knows what they are doing this can
>
>> be a powerful programming technique. I rarely see it used in those kind
>
>> of hands.:-)
>
>
>
> what David means is that most experienced programmers probably never
>
> even *needed* to create a variable dynamically.
>
>
>
> If the technique is available, this does not mean this technique is
>
> suitable for your problem. If you think you need X variables named
>
> var_0, var_1, ---, var_X than the algorithm you are thinking of is
>
> probably not the best one.
>
>
>
> In this case, a list (for IDL8+) or a pointer array (all versions) is
>
> probably a better solution.
>
>
>
> For example:
>
>
>
> var = PTRARR(25)
>
> for j=0, 24 do var[j]=PTR_NEW(whatever_you_want_to_put_inside)
>
>
>
> Fab
If anyone feels the inclination to cover this in more detail it'd be appreciated as I find myself using it quite often, normally as a shortcut to repeating the same thing over and over.
For example, say I have many different models datasets where I want to interact on the data on each individual model but I also want all the datasets to be available. I typically name the data modelA_variableA, modelB_variableA, etc and then loop over by setting thisModel_variableA=modelA_variableA using the type of execute command I posted above with "modelA" and "variableA" as the strings.
Is there a better (generic) way of doing this?
|
|
|