comp.lang.idl-pvwave archive
Messages from Usenet group comp.lang.idl-pvwave, compiled by Paulo Penteado

Home » Public Forums » archive » indexing a structure in a list
Show: Today's Messages :: Show Polls :: Message Navigator
E-mail to friend 
Switch to threaded view of this topic Create a new topic Submit Reply
indexing a structure in a list [message #87472] Wed, 05 February 2014 08:36 Go to next message
Helder Marchetto is currently offline  Helder Marchetto
Messages: 520
Registered: November 2011
Senior Member
Hi,
I've got the feeling this should work, but for some reason it isn't.
So here is some code to test the problem with the errors describe in it:

PRO testStrutRef
Compile_Opt idl2
a = list(findgen(10), /extract)
a[4] = {value:3.0}
help, a[4], /struct
;a[4].value = 4.0 ;won't work: % Attempt to store into an expression: Structure reference.
;(a[4]).value = 4.0 ;won't work: % Attempt to store into an expression: Structure reference.
;((a)[4]).value = 4.0 ;won't work: % Attempt to store into an expression: Structure reference.
struct = a[4]
struct.value = 4.0
a[4] = struct ;works, but not really straight forward...
print, a
END

It seems like I can't address a structure element of a list directly.
Did I misplace my parenthesis? Is there a less cumbersome workaround?

Thanks,
Helder
Re: indexing a structure in a list [message #87473 is a reply to message #87472] Wed, 05 February 2014 09:00 Go to previous messageGo to next message
David Fanning is currently offline  David Fanning
Messages: 11724
Registered: August 2001
Senior Member
Helder writes:

> I've got the feeling this should work, but for some reason it isn't.
> So here is some code to test the problem with the errors describe in it:
>
> PRO testStrutRef
> Compile_Opt idl2
> a = list(findgen(10), /extract)
> a[4] = {value:3.0}
> help, a[4], /struct
> ;a[4].value = 4.0 ;won't work: % Attempt to store into an expression: Structure reference.
> ;(a[4]).value = 4.0 ;won't work: % Attempt to store into an expression: Structure reference.
> ;((a)[4]).value = 4.0 ;won't work: % Attempt to store into an expression: Structure reference.
> struct = a[4]
> struct.value = 4.0
> a[4] = struct ;works, but not really straight forward...
> print, a
> END
>
> It seems like I can't address a structure element of a list directly.
> Did I misplace my parenthesis? Is there a less cumbersome workaround?

Yeah, don't know. I'm no expert, but I'm not surprised lists and
structures don't play well together. Have you thought about a hash
instead?

IDL> a = list(findgen(10), /extract)
IDL> a[4] = hash('value', 3.0)
IDL> (a[4])['value'] = 4.0
IDL> print, a[4]
value: 4.00000

Cheers,

David

--
David Fanning, Ph.D.
Fanning Software Consulting, Inc.
Coyote's Guide to IDL Programming: http://www.idlcoyote.com/
Sepore ma de ni thue. ("Perhaps thou speakest truth.")
Re: indexing a structure in a list [message #87474 is a reply to message #87472] Wed, 05 February 2014 09:52 Go to previous messageGo to next message
Craig Markwardt is currently offline  Craig Markwardt
Messages: 1869
Registered: November 1996
Senior Member
On Wednesday, February 5, 2014 11:36:09 AM UTC-5, Helder wrote:
> Hi,
>
> I've got the feeling this should work, but for some reason it isn't.
>
> So here is some code to test the problem with the errors describe in it:
>
>
>
> PRO testStrutRef
> Compile_Opt idl2
> a = list(findgen(10), /extract)
> a[4] = {value:3.0}
> help, a[4], /struct
> ;a[4].value = 4.0 ;won't work: % Attempt to store into an expression: >
> ;(a[4]).value = 4.0 ;won't work: % Attempt to store into an expression: Structure reference.
> ;((a)[4]).value = 4.0 ;won't work: % Attempt to store into an expression: Structure reference.
> struct = a[4]
> struct.value = 4.0
> a[4] = struct ;works, but not really straight forward...
> print, a
> END
>
> It seems like I can't address a structure element of a list directly.
> Did I misplace my parenthesis? Is there a less cumbersome workaround?

Seems like a flat-out bug to me. List elements should be "lvalues," as they say in the compile business.
Craig
Re: indexing a structure in a list [message #87475 is a reply to message #87474] Wed, 05 February 2014 10:05 Go to previous messageGo to next message
Michael Galloy is currently offline  Michael Galloy
Messages: 1114
Registered: April 2006
Senior Member
On 2/5/14, 10:52 AM, Craig Markwardt wrote:
> On Wednesday, February 5, 2014 11:36:09 AM UTC-5, Helder wrote:
>> Hi,
>>
>> I've got the feeling this should work, but for some reason it isn't.
>>
>> So here is some code to test the problem with the errors describe in it:
>>
>>
>>
>> PRO testStrutRef
>> Compile_Opt idl2
>> a = list(findgen(10), /extract)
>> a[4] = {value:3.0}
>> help, a[4], /struct
>> ;a[4].value = 4.0 ;won't work: % Attempt to store into an expression: >
>> ;(a[4]).value = 4.0 ;won't work: % Attempt to store into an expression: Structure reference.
>> ;((a)[4]).value = 4.0 ;won't work: % Attempt to store into an expression: Structure reference.
>> struct = a[4]
>> struct.value = 4.0
>> a[4] = struct ;works, but not really straight forward...
>> print, a
>> END
>>
>> It seems like I can't address a structure element of a list directly.
>> Did I misplace my parenthesis? Is there a less cumbersome workaround?
>
> Seems like a flat-out bug to me. List elements should be "lvalues," as they say in the compile business.
> Craig
>

Yes, seems like a bug. Note that index notation works:

IDL> print, (a[4]).(0)
3.00000

Mike
--
Michael Galloy
www.michaelgalloy.com
Modern IDL: A Guide to IDL Programming (http://modernidl.idldev.com)
Research Mathematician
Tech-X Corporation
Re: indexing a structure in a list [message #87477 is a reply to message #87475] Wed, 05 February 2014 11:42 Go to previous message
Helder Marchetto is currently offline  Helder Marchetto
Messages: 520
Registered: November 2011
Senior Member
On Wednesday, February 5, 2014 7:05:02 PM UTC+1, Mike Galloy wrote:
> On 2/5/14, 10:52 AM, Craig Markwardt wrote:
>
>> On Wednesday, February 5, 2014 11:36:09 AM UTC-5, Helder wrote:
>
>>> Hi,
>
>>>
>
>>> I've got the feeling this should work, but for some reason it isn't.
>
>>>
>
>>> So here is some code to test the problem with the errors describe in it:
>
>>>
>
>>>
>
>>>
>
>>> PRO testStrutRef
>
>>> Compile_Opt idl2
>
>>> a = list(findgen(10), /extract)
>
>>> a[4] = {value:3.0}
>
>>> help, a[4], /struct
>
>>> ;a[4].value = 4.0 ;won't work: % Attempt to store into an expression: >
>
>>> ;(a[4]).value = 4.0 ;won't work: % Attempt to store into an expression: Structure reference.
>
>>> ;((a)[4]).value = 4.0 ;won't work: % Attempt to store into an expression: Structure reference.
>
>>> struct = a[4]
>
>>> struct.value = 4.0
>
>>> a[4] = struct ;works, but not really straight forward...
>
>>> print, a
>
>>> END
>
>>>
>
>>> It seems like I can't address a structure element of a list directly.
>
>>> Did I misplace my parenthesis? Is there a less cumbersome workaround?
>
>>
>
>> Seems like a flat-out bug to me. List elements should be "lvalues," as they say in the compile business.
>
>> Craig
>
>>
>
>
>
> Yes, seems like a bug. Note that index notation works:
>
>
>
> IDL> print, (a[4]).(0)
>
> 3.00000
>
>
>
> Mike
>
> --
>
> Michael Galloy
>
> www.michaelgalloy.com
>
> Modern IDL: A Guide to IDL Programming (http://modernidl.idldev.com)
>
> Research Mathematician
>
> Tech-X Corporation

The index notation works only for printing, but not for assigning. But printing works also for
print, a[4].value

By the way:

IDL> !version
{
ARCH: "x86_64",
OS: "Win32",
OS_FAMILY: "Windows",
OS_NAME: "Microsoft Windows",
RELEASE: "8.3",
BUILD_DATE: "Nov 15 2013",
MEMORY_BITS: 64,
FILE_OFFSET_BITS: 64
}

Cheers,
Helder
  Switch to threaded view of this topic Create a new topic Submit Reply
Previous Topic: IDL as Art
Next Topic: How to make multiple plots on multiple pages with the same figure filename?

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ] [ PDF ]

Current Time: Wed Oct 08 11:43:52 PDT 2025

Total time taken to generate the page: 0.00326 seconds