Foreach philosphy [message #73720] |
Wed, 24 November 2010 12:55 |
wlandsman
Messages: 743 Registered: June 2000
|
Senior Member |
|
|
So I am trying to modernize my code by replacing FOR loops with the
FOREACH syntax when appropriate. That way one doesn't have to
worry about counting the number of elements, the code seems more
readable, and it even works for temporary arrays such as arr[*,
3,*].
But is it correct that you can't use the FOREACH syntax to update an
array? For example, is there way to use FOREACH for the
following code snippets?
IDL> for i=0,N_elements(a)-1 do a[i] = b[i]
or even
IDL> for i=0,N_elements(a)-1 do a[i] = 17
FOREACH doesn't give access to the counting index, and since array
elements are accessed by value, the following doesn't work
IDL> a = [1,2,3]
IDL> foreach element, a do element=17
IDL> print,a
1 2 3
|
|
|