Template Literal Strings

In IDL, you can create string constants using single or double quotes. You can also create template literal strings using backtick characters. These template literal strings can contain IDL expressions (the "templates") and can span multiple lines, with all whitespace and line breaks preserved in the final string (hence the word "literal"). At runtime, IDL evaluates all of the expressions within the template literal string, converts each expression to a string, and then concatenates all of the strings to produce a scalar IDL string constant.

 

For example:

a = [1, 2, 3, 4, 5]

result = `There are ${total(a gt 2, /int)} matches in the array ${a}.`

print, result

IDL prints:

There are 3 matches in the array [1,2,3,4,5].

 

As a multiline example, save the following code into a file called planets.pro:

pro planets

name = 'Jupiter'

mass = 1900e24

result = `Planet:

name: ${name}

mass: ${mass} kg`

print, result

end

Here, the template literal string spans three lines, and the second and third lines have multiple spaces at the beginning. After running this routine, IDL prints:

Planet:

name: Jupiter

mass: 1.9E+27 kg

You can see that IDL constructed the final string using the exact line breaks and spacing from the source code.

 

Template Literal String Expressions

To embed an IDL expression within a template literal string, use the following format:

${expression}

This expression can be any valid IDL expression, including variables, arithmetic, function calls, or even nested template literal strings. Once the expression is evaluated, the following rules are used to convert the result to a string:

 

Expression Result Conversion Example Input Example Output
Scalar integer Whitespace removed `${2 + (3 * 4)}` 14
Scalar float, double, or complex Whitespace and unnecessary trailing zeroes removed (except right after decimal) `${4 * atan(1)}` 3.1415927
Scalar string Unchanged `${"IDL is cool!"}` IDL is cool!
Array or List [value1,value2,...] `${[1, 2, 3]}` [1,2,3]
Hash or Dictionary {key1:value1,key2:value2,...} `${hash("a",5,"b",6)}` {a:5,b:6}
IDL object object.toString( ) `${BigInteger.Prime(40)}`

613747182329

!NULL String "!NULL" `${[ ]}` !NULL

Note: If the result is undefined then an error is thrown.

Note: If the result is an object that does not have a toString method then an error is thrown.

Expressions with Formats

In addition to the default formats, you can specify a format string to be used with your template expression, using the following syntax:

${expression,formatString}

Here, formatString is any valid IDL format string, using either FORTRAN or C-printf style format codes. For example:

print, `The value of Pi: ${!dpi,"%5.2f"}`

print, `The value of Pi: ${!dpi,"(f5.2)"}`

In both cases IDL prints:

The value of Pi: 3.14

For arrays, lists, or hashes, the format string is used to format each element, and then the elements are concatenated together as described in the table above. For example:

IDL> x = [1:5]

IDL> `The values are: ${x,"%5d"}`

The values are: [    1,    2,    3,    4,    5]

Since the format is just an IDL string, it can contain non-formatting characters as well:

IDL> `The values are: ${x,"...%d..."}`

The values are: [...1...,...2...,...3...,...4...,...5...]

You can even use a template literal string for the format, and dynamically construct the format string:

IDL> x=!dpi & i=25 & j=15

IDL> `The value of pi is ${x,`%-${i}.${j}f`}`

The value of pi is 3.141592653589793

Escape Characters

In addition to regular characters and template expressions, you can also embed the following characters using the backslash escape code:

Escape Code

ASCII Character

ASCII Code

\`

Backtick

96

\$ Dollar sign 36
\\ Backslash 92
\b

Backspace

8

\f Formfeed 12
\n Linefeed 10
\r

Carriage return

13

\t

Horizontal tab

9

\v

Vertical tab

11

\xXX

Hexadecimal code

XX

Note: The \xXX escape code lets you embed any character using a two-digit hexadecimal number in the range x00 to xFF. For characters in the extended ASCII range 128–255 (x80–xFF), IDL will embed the byte value in the string but the actual output character may look different on different output devices.

Note: Unrecognized escape codes will throw a syntax error.

Note: The escape code for dollar sign \$ is only necessary if the next character is a left brace { and you want to avoid invoking a template expression. Otherwise you can just embed dollar signs directly into your string without escaping them.

 

As an example of using escape characters:

name = 'New Horizons'

cost = 780.6e6

launch = 2006

result = `Spacecraft:\n\tName: ${name}\n\tCost: $${cost}\n\tYear: ${launch}`

print, result

IDL prints:

Spacecraft:

Name: New Horizons

Cost: $7.806E+08

Year: 2006

Version History

8.9.0

Introduced

See Also

Defining and Using Constants, STRING