Text manipulation with GAMAP

From Geos-chem
Revision as of 19:54, 15 April 2008 by Bmy (Talk | contribs) (Replacing characters in a string)

Jump to: navigation, search

Text strings in IDL

Creating strings

We may form a string of text characters in IDL either with the IDL's string function, or by placing the text between single quotes or double quotes. For example:

IDL> str1 = 'hello world'
IDL> help, str1
STR1            STRING    = 'hello world'
IDL> num2 = 3.14159
IDL> str2 = string( num2 )   
IDL> help, str2  
STR2            STRING    = '      3.14159'

Equivalence of strings and byte arrays

In IDL, a string of text characters is equivalent to an array of byte values. A byte is a collection of 8 bits and may express values from 0-255. The ASCII collating sequence has 255 values. (Actually, the original ASCII table had 128 values, but this was later extended to 255 values to include special characters.) One byte represents a single ASCII text character.

This means that it is easy to convert between strings and bytes in IDL. If you have an array of bytes, you can use any of the IDL string routines on them, for example:

IDL> byte_array = [ 72B, 69B, 76B, 76B, 79B ]
IDL> help, byte_array    
BYTE_ARRAY      BYTE      = Array[5]
IDL> print, strtrim( byte_array, 2 ) 

GAMAP comes with a very useful routine called str2byte.pro. This allows you to take a text string and to convert it into the equivalent array of bytes.

IDL> str = 'IDL is neat!'
IDL> byte_array = str2byte( str, strlen( str ) )
IDL> help, byte_array
BYTE_ARRAY      BYTE      = Array[12]
IDL> print, byte_array   
  73  68  76  32 105 115  32 110 101  97 116  33

Note that we used IDL's STRLEN function to return the length of the string.

Representing special characters

We must specify some special non-printing ASCII characters with their byte value. For exaaple, the horizontal tab character is the 9th character in the ASCII table, so we may specify that as:

IDL> tab = 9B
IDL> help, tab
TAB             BYTE      =    9
IDL> str = 'hello' + string(tab) + 'world' 
IDL> print, str
hello   world

For more information about IDL's string functions, please see http://idlastro.gsfc.nasa.gov/idl_html_help/Strings.html.

Replacing characters in a string

IDL's STRPUT function is one way to insert characters into a string of text:

IDL> str1 = 'Now is the winter of our discontent'
IDL> strput, str1, 'summer', 11
IDL> print, str1
Now is the summer of our discontent

However, this requires that you provide the location in the string where the text replacement will take place. In the above example, we insert the text at character 11 (the 1st character in a string is always character 0).

This is much more easily accomplished with GAMAP's REPLACE_TOKEN function:

IDL> str1 = 'Now is the winter of our discontent'
IDL> str2 = replace_token( str1, 'winter', 'summer', delim= )
IDL> print, str2
Now is the summer of our discontent

With REPLACE_TOKEN you do not need to know the position in the string where the replacement text will be inserted.

GAMAP also has another function called STRREPL that allows you to replace multiple instances of a single character in a string. For example:

IDL> print, strrepl( 'Mississippi', 'i', 'a' )

But if you need to replace an entire word rather than just single characters it's better to use REPLACE_TOKEN.