[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Array subscript question



"Kenneth P. Bowman" wrote:

> Can someone explain this behavior to me?  I can't find anything in the
> documentation that states that repeated subscripts are handled
> differently.
>
> IDL> a = FINDGEN(5)
> IDL> i = [1, 2, 3]
> IDL> a[i] = a[i] + 10.0
> IDL> PRINT, a
>       0.00000      11.0000      12.0000      13.0000      4.00000
>
> This is the behavior I expect.
>
> IDL> a = FINDGEN(5)
> IDL> i = [2, 2, 2]
> IDL> a[i] = a[i] + 10.0
> IDL> PRINT, a
>       0.00000      1.00000      12.0000      3.00000      4.00000
>
> Why does it only do the operation *once* when
> IDL> HELP, a[i]
> <Expression>    FLOAT     = Array[3]
>
> IDL> a = FINDGEN(5)
> IDL> i = [2, 2, 2]
> IDL> a[i] = a[i] + [10.0, 10.0, 10.0]
> IDL> PRINT, a
>       0.00000      1.00000      12.0000      3.00000      4.00000
>
> Even this doesn't help.
>
> Ken
>
>

The expression on the right is evaluated first. So your last example,

a[i] = a[i] + [10.0, 10.0, 10.0]

could be written as,

[ a[2], a[2], a[2] ] = [ a[2], a[2], a[2] ] + [10.0, 10.0, 10.0]

or,

[ a[2], a[2], a[2] ] = [ 12.0, 12.0, 12.0 ]

which is the same as,

a[2] = 12.0
a[2] = 12.0
a[2] = 12.0

So the operation does occur more than once, but it's just the same
operation. If you try a[i] = a[i] + [x, y, z], a[2] will equal 'z', since
that expression was executed last.

I hope this helps,
Steve Hartmann