Search code examples
listreferenceexpressionstructureidl

Attempt to store into an expression: Structure reference


I've got the following problem in IDL language (I have different size arrays in a list of structures and I try to change the values in those arrays, but it gives me the error called "Attempt to store into an expression: Structure reference."). Example code:

a = list()
a.add,{a: 1, b: findgen(10)}
a.add,{a: 2, b: findgen(20)}
a[0].b[5] = 2
% Attempt to store into an expression: Structure reference.

Could anybody tell me, what am I doing wrong? Is there a workaround for this? I haven't found any useful tips for this situation so far.

Thanks in advance!

EDIT: I use IDL 8.2.1

EDIT2: It does not work with IDL 8.4 either


Solution

  • I'm an IDL novice myself, but I think what is happening when you access a structure in a list, is that the list is returning a copy of the structure. When this copy is on the left-hand side, it is a temporary, and IDL is preventing the assignment to a temporary.

    Try this:

    a = list()
    a.add,{a: 1, b: findgen(10)}
    a.add,{a: 2, b: findgen(20)}
    c = a[0]
    c.b[5] = 2
    a[0] = c
    print, a[0].b
    0.000000      1.00000      2.00000      3.00000      4.00000      2.00000      6.00000      7.00000      8.00000      9.00000
    

    The problem with this is that it's cumbersome and expensive to make a copy of the struct. Maybe there is a better way to handle this?

    Edit: perhaps this Google discussion will provide some insight:

    Modifying Arrays and Structures in HASH's (hint: you can't)