Accessing numbered arrays [message #93042] |
Thu, 14 April 2016 04:55  |
Richard Morton
Messages: 7 Registered: April 2016
|
Junior Member |
|
|
Hi,
I have a routine (not mine) that creates numbered arrays as output, i.e., array1 , array2, array3, etc.
I would like to access these arrays sequentially within a for loop.
I am currently using:
FOR i=0,n DO temp=scope_varfetch('array'+strtrim(i,2)) .
This does the job but is this an appropriate way to do it? From my reading about scope_varfetch it appears to be designed for accessing variables at different levels of the code. This is not what I doing, so I wondered if there was another method.
If a similar question has been answered before I apologise, I couldn't find anything.
Cheers,
Richard
|
|
|
Re: Accessing numbered arrays [message #93043 is a reply to message #93042] |
Thu, 14 April 2016 05:56   |
wlandsman
Messages: 743 Registered: June 2000
|
Senior Member |
|
|
Traditionally, one would use EXECUTE() for this.
There might be cases where using EXECUTE() would be faster because one doesn't necessarily need to make a copy of the array. For example, if all you need is the maximum of the array then
res = EXECUTE('amax = max(temporary(array' + strtrim(i,2) + '))' )
gets the maximum, and deletes the array without ever having to make a duplicate copy.
But I think for most cases SCOPE_VARFETCH would work just as well. (Also note that EXECUTE() can't be used with the virtual machine, but SCOPE_VARFETCH can).
On Thursday, April 14, 2016 at 7:55:57 AM UTC-4, Richard Morton wrote:
> Hi,
>
> I have a routine (not mine) that creates numbered arrays as output, i.e., array1 , array2, array3, etc.
>
> I would like to access these arrays sequentially within a for loop.
>
> I am currently using:
>
> FOR i=0,n DO temp=scope_varfetch('array'+strtrim(i,2)) .
>
> This does the job but is this an appropriate way to do it? From my reading about scope_varfetch it appears to be designed for accessing variables at different levels of the code. This is not what I doing, so I wondered if there was another method.
>
> If a similar question has been answered before I apologise, I couldn't find anything.
>
> Cheers,
> Richard
|
|
|
|
|
Re: Accessing numbered arrays [message #93048 is a reply to message #93045] |
Fri, 15 April 2016 08:11  |
wlandsman
Messages: 743 Registered: June 2000
|
Senior Member |
|
|
On Friday, April 15, 2016 at 3:46:40 AM UTC-4, Richard Morton wrote:
> Great, thanks for the advice. In this case I need the entire array but will bear EXECUTE in mind for other tasks.
Just a quick note that you can get the entire array with EXECUTE(). Instead of
FOR i=0,n DO temp=scope_varfetch('array'+strtrim(i,2))
you would write
FOR i=0,n DO res=execute('temp = array'+strtrim(i,2))
|
|
|