Getting Started Documentation Glish Learn More Programming Contact Us
Version 1.9 Build 1556
News FAQ
Search Home


next up previous contents index
Next: Manipulating Symbols Up: Predefined Functions and Variables Previous: Value Creators

Subsections



String Functions

split and paste

paste takes a list of values, converts them all to scalar string's, and returns their concatenation as a scalar string value. For example,
    a := [2,3,5]
    paste( "the first three primes are", a )
yields
    the first three primes are [2 3 5]
The []'s seen here in the string representation of the vector a only occur for a numeric value with more than one element.

Similarly,

    paste( "hello", "there" )
is equivalent to the string constant
    'hello there'

By default, the string values are concatenated together using a single space. The optional sep= argument specifies a string to be used instead. For example,

    paste("hello", "there", "how", "are", "you?",
          sep="XYZ")
yields
    helloXYZthereXYZhowXYZareXYZyou?

Note that the arguments to paste are first converted to scalar string's, and then concatenated together. So

    paste( "hello there", 1:3, sep="" )
yields
    hello there[1 2 3]
and not
    hellothere[123]

If a single element string is passed to paste, the elements of the string are concatenated. For example:

    paste("a b c d e", sep='' )
results in the string "abcde". With multiple strings, the individual strings are concatenated, but not the individual elements of the strings.

spaste is simply a version of paste with the separator set to an empty string. It is defined using:

    func spaste(...) paste(...,sep='')
This form of paste is common enough that it merits its own simple form.

split is basically the inverse of paste. It takes a single argument, converts it to a scalar string, and splits it into words at each block of whitespace, just as string constants are constructed when enclosed in double-quotes (see § 3.3.1, page [*]). Thus

    split('hello there how are you?')
is equivalent to
    "hello there how are you?"
that is, it yields a five-element string vector.

You can also call split with a second argument. The second parameter specifies where in the string the break should occur, for example

    split("hello   there how are you", "eo")
yields the equivalent of
    ['h', 'll', ' th', 'r', ' h', 'w ar', ' y', 'u']
Here the first element is 'h', the second is 'll', the third ' th', and so forth. The presence of the single leading space in ' th' may be surprising. What happened is that first split converted
    "hello   there how are you"
to a scalar value, equivalent to
    'hello there how are you'
since the double-quoted constant was constructed all information about the number of blanks between words was lost. Next split broke the scalar into words at every occurrence of an 'e' or an 'o', but not at each blank like it would without the second argument. If a blank had been included in the second argument then these extra blanks naturally disappear:
    split("hello   there how are you", 'eo ')
yields the equivalent of
    "h ll th r h w ar y u"
Note that you have to enclose the second argument in single-quotes, otherwise the blank will be removed.

If the second argument is a null string, the first string is broken up into individual characters. Here is an example:

    split("how are you", '')
yields the string:
    ['h', 'o', 'w', ' ', 'a', 'r', 'e', ' ', 'y', 'o', 'u']

sprintf

The sprintf10.1 function is available for generating strings. This function takes the same arguments as the stdio.h sprintf function. This example:

    sprintf("result: 0x%x", 2871)
would return the string 'result: 0xb37'. It uses the %x field specifier to output a hexadecimal number. %x is just one of the possible field specifiers available with sprintf (or printf or fprintf); Table 10.1 lists all of the possible field specifiers. Each of these field specifiers is prefixed with a percent sign and indicates a field in the output string which is replaced with a subsequent sprintf argument. Each field is replaced with the respective argument following the format string. Here is another example:
    sprintf("%4d/%02d/%02d %d:%02d:%02d",1998,8,20,7,46,2)
In this case, a string like '1998/08/20 7:46:02' would be returned. This example uses the %d field specifier to create a date and time string. %% is used to put a percent sign into the result, like:
    sprintf("%d%%", 100)
which would create the string '100%'. In each case, the result of sprintf is a string made up of the format string with each field specifier replaced with a subsequent argument.
Table 10.1: sprintf Fields Specifiers
Field Specifer Input Output
%d integer, short, byte decimal
%x integer, short, byte hexadecimal (uses a-f)
%X integer, short, byte hexadecimal (uses A-F)
%o integer, short, byte octal
%r integer, short, byte roman numeral (uses m,d,c,l,x,v,i)
%R integer, short, byte roman numeral (uses M,D,C,L,X,V,I)
%s string string
%c string, integer, short, byte single character
%f float, double floating point
%e float, double exponential notation
%g float, double the shorter of %f or %e
%% none %


Each field specifier can have an extra qualifier between the % and the conversion character. The format of this qualifier is:

minus sign
if present, indicates that the converted argument should be left adjusted in its field. Right adjusted output is the default.

digit string
if present, specifies the minimum field width. In the case of a number, it will be printed in a field at least this long; the field is widened if it is too small for the argument. The argument will be padded if it is smaller than the specified field width. By default, the pad character is a space, but if this digit string begins with a zero, then zero is used as the pad character.

period
is used to seperate the width digit string from the next digit string.

digit string
specifies the number of printed digits digits to the right of the decimal point for floating point numbers or the maximum number of characters for strings.
Each of these qualifier components is optional, though the period must always be used in conjunction with the final digit string. Table 10.1 has some examples.


Table 10.2: sprintf Examples
Command Result
sprintf(">%.7s<",'long string') >long st<
sprintf(">%7s<",'long string') >long string<
sprintf(">%20s<",'long string') > long string<
sprintf(">%-20s<",'long string') >long string <
sprintf(">%-20.7s<",'long string') >long st <
sprintf(">%-20.7f<",pi) >3.1415927 <
sprintf(">%20.3f<",pi) > 3.142<
sprintf(">%020.3f<",pi) >0000000000000003.142<
sprintf(">%20.6e<",pi) > 3.141593e+00<


If sprintf is applied to vectors, the format string is applied to each element of the vectors. For example:

    print sprintf('%d\t%r\n',1998:2002,1998:2002)
would result in the following output:
    1998    mcmxcviii
     1999   mcmxcix
     2000   mm
     2001   mmi
     2002   mmii
The result of sprintf is a string, and the misalignment of 1998 and 1999 is the result of the space added to separate elements of a string. spaste, for example, could be used to get rid of this misalignment, or printf could be used instead of sprintf with print.

tr

The tr function can be used to translate strings. It takes two ranges plus a string, and it maps characters of the first range occurring in a string to the corresponding character of the second range. Here are a couple of simple examples:

    tr('[a-z]','[A-Z]',"This is a string.")
    tr('[a-z]','[Z-A]',"This is a string.") 
    tr('[a-m][n-z][A-M][N-Z]','[n-z][a-m][N-Z][A-M]',"This is a string.")
The result of these examples would be:
    THIS IS A STRING. 
    TSRH RH Z HGIRMT. 
    Guvf vf n fgevat.
The first example capitalizes all of the letters, the second jumbles them, and the third performs Rot13.

There are two other convienence functions based on tr, to_lower() and to_upper(). These functions convert a string to lower case or upper case.

strlen

strlen returns the length of each element of a string. For example:
    strlen("the cow jumped over the moon")
would return:
    [3 3 6 4 3 4]


next up previous contents index
Next: Manipulating Symbols Up: Predefined Functions and Variables Previous: Value Creators   Contents   Index
Please send questions or comments about AIPS++ to aips2-request@nrao.edu.
Copyright © 1995-2000 Associated Universities Inc., Washington, D.C.

Return to AIPS++ Home Page
2006-10-15