Coyote's Guide to IDL Programming

Concatenating IDL Structures

QUESTION: I have no trouble concatenating named structures, but I get the error message % Conflicting data structures when I try to concatenate anonymous structures. Can you explain why this happens?

ANSWER: When you concatenate items in an array they have to be the same kind of item (or be easily converted to the same kind of item). So, for example, this doesn't work because the variables a and b have different dimensions:

   IDL> a = [2, 3] 
   IDL> b = [[2,3],[4,5]]
   IDL> c = [a, b]
      % Unable to concatenate variables because the dimensions do not agree: B.

The same is true of structures. There is no trouble with named structures because a named structure is by definition defined identically for each instance of the structure. So, for example, this works easily:

   IDL> a = {EMPLOYEE, Name:'Larry', Age:46} 
   IDL> b = {EMPLOYEE, Name:'JoeBob', Age:39}
   IDL> c = [a, b]

What is not so obvious is why this doesn't work with anonymous structures. For example, this code fails:

   IDL> a = {Name:'Larry', Age:46} 
   IDL> b = {Name:'JoeBob', Age:39}
   IDL> c = [a, b]
      % Conflicting data structures: B,concatenation.

It seems that anonymous structures in IDL are given "invisible" names. You can see what these names are by typing this command:

   IDL> Help, a, b, /Structure
      ** Structure [f19348], 2 tags, length=12, refs=1:
      NAME            STRING    'Larry'
      AGE             INT             46
      ** Structure [f19c98], 2 tags, length=12, refs=1:
      NAME            STRING    JoeBob
      AGE             INT             39

In this example, the anonymous structures have the names f19348 and f19c98. The fact that these two anonymous structures are different "things" to IDL keeps them from being concatenated.

However, if you wanted to be able to concatenate anonymous structures, you could do this:

   IDL> a = {Name:'Larry', Age:46} 
   IDL> b = a
   IDL> b.Name = 'JoeBob'
   IDL> b.Age = 39
   IDL> c = [a, b]

This works because the structures a and b are now the same thing:

   IDL> Help, a, b, /Structure
      ** Structure [f19e98], 2 tags, length=12, refs=3:
      NAME            STRING    'Larry'
      AGE             INT             46
      ** Structure [f19e98], 2 tags, length=12, refs=3:
      NAME            STRING    'JoeBob'
      AGE             INT             39

Changes in IDL 6.0

Note that this is no longer a problem starting with IDL 6.0. It is now possible to concatenate anonymous structures as long as they have the same field sizes and types. That is to say, they do not even have to have the same field names. For example, consider this code, written in IDL 6.2.

   IDL> a = {Name: 'Larry', Age: 46}
   IDL> b = {LastName: 'Henderson', No_of_Dogs: 4}
   IDL> c = [ a, b]
   IDL> Help, c
   C               STRUCT    = ->  Array[2]

[Return to IDL Programming Tips]