13224 Rate this article:
3.6

OrderedHash Data Type: A New Feature in IDL 8.3

Jim Pendleton
Before Dr. Mark Piper took up the next phase of his stellar career, he shepherded the engineering team through the IDL 8.3 development process. He's left us with a big bag of new features and other goodies that you can now unwrap.

I'm particularly jazzed about the new language features that will help me, in my role as an application developer, to write more efficient,compact and maintainable code.

Since the introduction of the HASH data type in IDL 8.0, and its upgrades in 8.1 and 8.2, its use has spread like wildfire in the work we in the Professional Services Group write for our customers. It's been key (pun intended) to many imaginative solutions for complex problems.

The default HASH data type's storage order is arbitrary. As you add key/value pairs,they're stored internally not in an ordered list, but in a data organization that maximizes efficiency for subsequently accessing your values via their keys. If you request the key list from your HASH, the order of the keys may not resemble in any way the order in which you added the key/value pairs to the HASH:

IDL> keys = ['A', 'B', 'C', 'D', 'E', 'F', 'G']

IDL> values = LIST('one', 2.0, 3, 4l, PTR_NEW(5), {n:6}, COMPLEX(7,0))

IDL> hash = HASH(keys, values)

IDL> PRINT, hash.Keys()

IDL prints:

A F C D G B E

More often than not, this is perfectly acceptable behavior.

However, if the key order itself is important, prior to IDL 8.3 you would need to write additional code to manage your key indices independently. Sometimes a simple sort of the keys is sufficient, as is the case in this example, but at other times a more complex data organization is required.

In  IDL 8.3, the development team has introduced the new ORDEREDHASH data type. Its behavior is essentially identical to HASH except that it performs the bookkeeping internally to remember the order in which items were added to the hash, and will return the keys in that order.

IDL> ohash = ORDEREDHASH(keys, values)

IDL> PRINT, ohash.Keys()

IDL prints:

A B C D E F G

This change is relevant for those of us who use JSON notation,for example in ENVI Services Engine task development.  As of IDL 8.3, the JSON_PARSE function will return ORDEREDHASH objects rather than vanilla HASH objects.  The result is that the order of objects (lists and hashes) in the returned nested data structure will match the order of items in the original JSON stream.

If you're not familiar with JSON, you may well see it again in 8.3 even if you don't know it by name. In the context of the new implied print functionality, it's the standard output format for the LIST and HASH data types.  This also applies to ORDEREDHASH and any other subclasses that may be derived in the future.

More about implied print in a future article!

Here's a bonus feature for you in IDL 8.3.  You can now cut and paste example code that includes the "IDL>" prompt, as shown above for example, directly to the IDL command line.  The command line parser will recognize the prompt portion of the string, strip it off, and execute the remainder of the line.

Please login or register to post comments.