CONVERT Statement

The CONVERT statement may be used to replace characters in a string variable.

Format

CONVERT expr1 TO expr2 IN var

Parameter(s)

expr1

List of original characters to be converted.

expr2

List of characters to replace original characters.

var

String variable to be converted.

Description

The CONVERT statement replaces every occurrence of each of the specified characters with the corresponding replacement character. It treats each expression as a list of characters, not as a string: the first character in expr1 is replaced with the first character in expr2, the second character in expr1 is replaced with the second character in expr2, and so on.

Every time a character listed in expr1 appears in the string, it is replaced by the corresponding replacement character, regardless of how many times it appears. If expr1 contains more characters than expr2, the extra characters are deleted from the converted string. If the second expression contains more characters than the first, the extra characters in expr2 are ignored. If a character is repeated in the first expression, only the first assignment is made and all subsequent assignments of that character are ignored.

Examples

If the variable STRING contains "I LIKE IT", all Ks may be changed to Vs with:

CONVERT "K" TO "V" IN STRING

The resulting string is "I LIVE IT". However,

CONVERT "LIKE" TO "LOVE" IN STRING

produces the string, "O LOVE OT".

In the next application the CONVERT statement is used to turn a comma-separated list of names, NAMES, into a dynamic array, by converting each comma into an attribute mark.

EQUATE AM TO CHAR(254)

   .

   .

   .

CONVERT "," TO AM IN NAMES

As a comma-separated list, fields of the NAMES array may be deleted, inserted, or arranged only through a sequence of statements involving FIELD, COL1( ), etc. By converting commas to attribute marks, however, fields may be manipulated using the more powerful (and more intuitive) dynamic array functions.

See Also

Statement and Function Reference