New (CharacterMap constructor)

From m204wiki
Jump to navigation Jump to search

Create CharacterMap object (CharacterMap class)

The New method creates a character map, which associates individual characters from an In string (the "input table") with the characters in the corresponding position in the "output table". The output table consists of the characters in the Out string (plus copies of the Pad character, if necessary).


%characterMap = [%(CharacterMap):]New( In= string, [Out= string], [Pad= c])

Syntax terms

%characterMap A CharacterMap object variable.
[%(CharacterMap):] The optional class name in parentheses denotes a Constructor. See "Usage notes", below, for more information about invoking a CharacterMap Constructor.
In A required, name required, argument that specifies the "input table" characters in the mapping. No character duplications are allowed in the In string, which may not be more than 256 characters.
Out An optional, name required, argument that specifies the "output table" characters. Out may not be longer than In; it may be shorter, however, as long as a pad character is specified. Out may not be more than 256 characters.
Pad An optional, name required, argument that specifies the character used to pad the output table string on the right if it is shorter than In.

Pad must be a single character, only.

Usage notes

  • As described in "Using New or other Constructors", New can be invoked with no object, with an explicit class name, or with an object variable in the class, even if that object is Null:

    %charMap = new(in='A', out='-') %charMap = %(CharacterMap):new(in='A', out='-') %charMap = %charMap:new(in='A', out='-')

  • Case is respected in all character specifications.
  • If the In string is longer than the Out string, the "extra" In characters are associated with the Pad character. In this case, it is a request-canceling error if no Pad character is present.
  • The Out argument can be omitted, as long as a Pad argument is present. The Pad argument has no default.


Most of the CharacterMap methods are shown in the following example, which also features the intrinsic String function, Translate. In the example, a new CharacterMap is the argument for the Translate method; then that map is copied and modified, and the modified map is used in a second Translate call:

begin %map is object characterMap %map2 is object characterMap %ls is longstring %map = New(in='x-', out='!c') %ls = 'xu--exx' printtext {~} = '{%ls:translate(%map)}' %map2 = %map:Copy %map2:Update(in='x',out='s') printtext {~} = '{%ls:translate(%map2)}' end

The result is:

%ls:translate(%map) = '!ucce!!' %ls:translate(%map2) = 'success'

See also