Delete element from the structure inside another structure [message #88936] |
Sun, 06 July 2014 16:37  |
Andrii
Messages: 7 Registered: March 2014
|
Junior Member |
|
|
Hello everybody!
I'd like to ask you about one thing which is generally already discussed in other groups.
But my question is a bit more specific.
I have a structure inside another structure like this one:
a = { f1: 0, f2: { x: FltArr(10,10,10), y: 0}}
My question is, how can I delete just an array x, or to replace an array x with the array y = FltArr(5,5,5)?
Thank you!
|
|
|
Re: Delete element from the structure inside another structure [message #88943 is a reply to message #88936] |
Mon, 07 July 2014 01:50   |
greg.addr
Messages: 160 Registered: May 2007
|
Senior Member |
|
|
Hi Andrii,
I think you can't do that directly. You have two choices: either create a new structure in the shape you want, and explicitly copy over the elements you want to retain (struct_assign), or -- probably the better solution -- use a pointer in the first place, so that:
a = { f1: 0, f2: { x: ptr_new(FltArr(10,10,10)), y: 0}}
then you could set a.f2.x=ptr_new(FltArr(5,5,5)) and access with
print,*(a.f2.x)
If you want to be able to change all the values, put the pointers at the lowest level, and recreate the structures each time:
a=ptrarr(2)
a[0]=ptr_new(0)
a[1]=ptr_new({ x: ptr_new(FltArr(10,10,10)), y: 0})
or
a[1]=ptr_new({ x: ptr_new(FltArr(5,5,5)), y: 0})
cheers,
Greg
|
|
|
Re: Delete element from the structure inside another structure [message #88944 is a reply to message #88943] |
Mon, 07 July 2014 01:57   |
Andrii
Messages: 7 Registered: March 2014
|
Junior Member |
|
|
Hi Greg,
Thanks a lot. I will try!
Cheers,
Andrii
> Hi Andrii,
>
>
>
> I think you can't do that directly. You have two choices: either create a new structure in the shape you want, and explicitly copy over the elements you want to retain (struct_assign), or -- probably the better solution -- use a pointer in the first place, so that:
>
>
>
> a = { f1: 0, f2: { x: ptr_new(FltArr(10,10,10)), y: 0}}
>
>
>
> then you could set a.f2.x=ptr_new(FltArr(5,5,5)) and access with
>
>
>
> print,*(a.f2.x)
>
>
>
> If you want to be able to change all the values, put the pointers at the lowest level, and recreate the structures each time:
>
>
>
> a=ptrarr(2)
>
> a[0]=ptr_new(0)
>
> a[1]=ptr_new({ x: ptr_new(FltArr(10,10,10)), y: 0})
>
>
>
> or
>
>
>
> a[1]=ptr_new({ x: ptr_new(FltArr(5,5,5)), y: 0})
>
>
>
> cheers,
>
> Greg
|
|
|
|
Re: Delete element from the structure inside another structure [message #88946 is a reply to message #88945] |
Mon, 07 July 2014 02:27   |
Andrii
Messages: 7 Registered: March 2014
|
Junior Member |
|
|
Thanks!
Could you please also write me, how to completely delete a[1] ?
If I want to print x, should I do it in the next way: print,*a[1].x ?
> actually, I should have written:
>
>
>
> a=ptrarr(2)
>
> a[0]=ptr_new(0)
>
> a[1]=ptr_new({ x: FltArr(10,10,10), y: 0})
>
>
>
> or
>
>
>
> a[1]=ptr_new({ x: FltArr(5,5,5), y: 0})
>
>
>
> greg
|
|
|
|
Re: Delete element from the structure inside another structure [message #88948 is a reply to message #88947] |
Mon, 07 July 2014 02:49   |
Fabzi
Messages: 305 Registered: July 2010
|
Senior Member |
|
|
If you have IDL8+ you can use dictionnaries instead of structures:
IDL> a = dictionary('f1', 0, 'f2', dictionary('x', FltArr(3), 'y', 0))
IDL> print, a.f2.x
0.00000 0.00000 0.00000
IDL> a.f2.x = 2
IDL> print, a.f2.x
2
IDL> ok = (a.f2)->remove('x')
IDL> print, a.f2.x
% Key does not exist: "X"
% Execution halted at: $MAIN$
Cheers,
Fabien
|
|
|
|