|
|
Re: newbie question / concatenation of arrays of nested structure [message #17047 is a reply to message #17045] |
Wed, 08 September 1999 00:00  |
Thomas A. McGlynn
Messages: 23 Registered: March 1996
|
Junior Member |
|
|
If you don't want to modify the code that is creating the structures
to be concatenated then neither of these approaches might be feasible. You
could then do something like:
first = replicate({...}, ...)
....
second = replicate({...}, ...)
....
temp = replicate(first[0], n_elements(second));
for i=0, n_tags(first[0])-1 do temp.(i) = second.(i)
first = [first,temp]
This still isn't too bad, just three lines for a reasonably generic solution.
So long as the types agree you don't even have to worry about the
structure tag names matching. One could make build this into a function
easily enough -- though a general routine would probably need to deal
with recursive structures intelligently (and maybe multi-dimensional
arrays of structures).
Regards,
Tom McGlynn
Liam Gumley wrote:
>
> Liam Gumley wrote:
>> The only way to create equivalent structures is to use named structures,
>> e.g.
>>
>> IDL> record = {z, a:0, b:'name', c:0}
>> IDL> first = replicate(record, 5)
>> IDL> second = replicate(record, 3)
>> IDL> combo = [first, second]
>
> Note to self: Any time you say "The only way" in this newsgroup, you're
> bound to be wrong.
>
> David's web page correctly points out that copies of an anonymous
> structure are equivalent, and thus can be concatenated, e.g.
>
> IDL> record = {a:0, b:'name', c:{d:0, e:0}}
> IDL> a = record
> IDL> b = record
> IDL> c = [a, b]
>
> Cheers,
> Liam.
>
> --
> Liam E. Gumley
> Space Science and Engineering Center, UW-Madison
> http://cimss.ssec.wisc.edu/~gumley
|
|
|
Re: newbie question / concatenation of arrays of nested structure [message #17048 is a reply to message #17047] |
Wed, 08 September 1999 00:00  |
Liam Gumley
Messages: 473 Registered: November 1994
|
Senior Member |
|
|
Thomas Launey wrote:
> how can I concatenate 2 arrays of anonymous nested structures?
> In other word, try to concatenate the two arrays below
>
> first=replicate({a:0,b:'name',c:{d:0,e:0}},5)
> second=replicate({a:0,b:'name',c:{d:0,e:0}},3)
> third=[first,second]
> % Conflicting data structures: B,concatenation.
> % Execution halted at: $MAIN$
Since first and second are anonymous structures, IDL has no way of
knowing that they contain the same fields, e.g.
IDL> first = replicate({a:0,b:'name',c:{d:0,e:0}},5)
IDL> second = replicate({a:0,b:'name',c:{d:0,e:0}},3)
IDL> help, first, /structure
** Structure <1373ab8>, 3 tags, length=16, refs=1:
A INT 0
B STRING 'name'
C STRUCT -> <Anonymous> Array[1]
IDL> help, second, /structure
** Structure <13734c8>, 3 tags, length=16, refs=1:
A INT 0
B STRING 'name'
C STRUCT -> <Anonymous> Array[1]
You can see that the structure ids are different (1373ab8 vs. 13734c8).
IDL uses the structure id to determine if structures are equivalent.
The only way to create equivalent structures is to use named structures,
e.g.
IDL> record = {z, a:0, b:'name', c:0}
IDL> first = replicate(record, 5)
IDL> second = replicate(record, 3)
IDL> combo = [first, second]
IDL> help, first, /structure
** Structure Z, 3 tags, length=16:
A INT 0
B STRING 'name'
C INT 0
IDL> help, second, /structure
** Structure Z, 3 tags, length=16:
A INT 0
B STRING 'name'
C INT 0
Now first and second have the same structure id (z), and they can be
concatenated. Note that IDL does not like nested anonymous structures
inside a named structure, e.g.
IDL> record = {x, a:0, b:'name', c:{d:0, e:0}}
% Structures can't have anonymous structure members
% Execution halted at: $MAIN$
so you may have to re-think your original record format.
Cheers,
Liam.
--
Liam E. Gumley
Space Science and Engineering Center, UW-Madison
http://cimss.ssec.wisc.edu/~gumley
|
|
|
Re: newbie question / concatenation of arrays of nested structure [message #17049 is a reply to message #17047] |
Wed, 08 September 1999 00:00  |
Liam Gumley
Messages: 473 Registered: November 1994
|
Senior Member |
|
|
Liam Gumley wrote:
> The only way to create equivalent structures is to use named structures,
> e.g.
>
> IDL> record = {z, a:0, b:'name', c:0}
> IDL> first = replicate(record, 5)
> IDL> second = replicate(record, 3)
> IDL> combo = [first, second]
Note to self: Any time you say "The only way" in this newsgroup, you're
bound to be wrong.
David's web page correctly points out that copies of an anonymous
structure are equivalent, and thus can be concatenated, e.g.
IDL> record = {a:0, b:'name', c:{d:0, e:0}}
IDL> a = record
IDL> b = record
IDL> c = [a, b]
Cheers,
Liam.
--
Liam E. Gumley
Space Science and Engineering Center, UW-Madison
http://cimss.ssec.wisc.edu/~gumley
|
|
|
Re: newbie question / concatenation of arrays of nested structure [message #17050 is a reply to message #17047] |
Wed, 08 September 1999 00:00  |
davidf
Messages: 2866 Registered: September 1996
|
Senior Member |
|
|
Thomas Launey (t_launey@brain.riken.go.jp) writes:
> This question has probably been already posted many time.
> how can I concatenate 2 arrays of anonymous nested structures?
> In other word, try to concatenate the two arrays below
>
> first=replicate({a:0,b:'name',c:{d:0,e:0}},5)
> second=replicate({a:0,b:'name',c:{d:0,e:0}},3)
> third=[first,second]
> % Conflicting data structures: B,concatenation.
> % Execution halted at: $MAIN$
>
> I have not been able to find a way to do that properly (without subscripting
> each field one after another) an there are no example in David Fanning's
> book (Hey David, I've bought your great book, please help :-))
Not everything is in the book. Sometimes you have to
visit my web page. :-)
http://www.dfanning.com/tips/concatenate_structs.html
Cheers,
David
--
David Fanning, Ph.D.
Fanning Software Consulting
Phone: 970-221-0438 E-Mail: davidf@dfanning.com
Coyote's Guide to IDL Programming: http://www.dfanning.com/
Toll-Free IDL Book Orders: 1-888-461-0155
|
|
|