SheerPower® 4GL
A Guide to the SheerPower Language


Previous Contents Index

6.4.4 CHR$(int_expr1[, int_expr2])

CHR$ returns a string with the specified ASCII value (int_expr1) repeated the specified number of times (int_expr2). If no count is specified, a default count of one is used.

Example 6-52 CHR$ function

  x = 65 
  print chr$(x)  // prints A -- the 65th ASCII character 
  end
  
  
A 

6.4.5 CONVERT$(int_expr1 [[, int_expr2], int_expr3])

Given an integer (int_expr1) and an optional length (int_expr2), which defaults to four, the CONVERT$ function returns a string mapping of the integer.

If the optional data type (int_expr3) is 17, the returned string will be a packed floating (PF).

The following data types are supported:

Table 6-5 CONVERT$ function - supported data types
Data Type Conversion Result
1 Integer (2 or 4 byte)
7 COBOL comp-3 (C3 packed decimal)
17 Packed floating (PF)

Example 6-53 CONVERT$ function - supported data types

  a$ = convert$(16961) 
  print a$ 
  end
        
 
AB 

6.4.6 CONVERT(str_expr)

Given a string containing a mapped integer, the CONVERT function returns the integer value.

Example 6-54 CONVERT function

  a$ = 'AB' 
  b = convert(a$) 
  print b 
  end
 
 
16961 

The CONVERT and CONVERT$ functions can be used in situations such as building segmented keys consisting of multiple data types.

6.4.7 CPAD$(text_str, size [, pad_str])

CPAD$ returns a new string, padded on the left and on the right with pad characters. text_str is the string to be centered, size is the size of the new string. The default pad character is a space.

Example 6-55 CPAD$ function

  print cpad$('123', 9, '0') 
  end
 
 
000123000 

6.4.8 EDIT$(str_expr,int_expr)

EDIT$ performs one or more editing operations on the supplied string argument, depending on the value of the integer expression. The integer expression is one of the integers below, or a sum of integers below for the desired edit functions:

Table 6-6 EDIT$ function - operation values
Value Edit Operation
1 Trim parity bits.
2 Discard all spaces and tabs.
4 Discard characters: CR, LF, FF, ESC, RUBOUT and NULL.
8 Discard leading spaces and tabs.
16 Reduce spaces and tabs to One space.
32 Convert lower case to upper case.
64 Convert "[" to "(" and "]" to ")".
128 Discard trailing spaces and tabs.
256 Do not alter characters inside quotes.

Example 6-56 EDIT$ function

  print edit$('hi there, how are you today?' , 32) 
 
 
HI THERE, HOW ARE YOU TODAY?  

6.4.9 ELEMENTS(str_expr1 [, str_expr2])

The ELEMENTS function returns the number of elements in a string expression that contains a list of elements. str_expr1 is the string containing the list of elements. str_expr2 is the separator. A comma is the default separator.

A given element is considered quoted only if the first non-blank character of the element is a single or double quote mark.

Example 6-57 ELEMENTS function

  alphabet$ = 'a;b;c;d;e;f;g;h;i;j;k;l;m;n;o;p;q;r;s;t;u;v;w;x;y;z' 
  print elements(alphabet$, ';') 
  end
  
  
26 

6.4.10 ELEMENT$(str_expr1, num_expr [, str_expr2])

ELEMENT$ returns the element from str_expr1 which is specified by the num_expr. str_expr1 contains a set of elements with separators between them. The default separator is a comma:

Example 6-58 ELEMENT$ function

  let a$ = element$('ADD,DEL,EXIT',2) 
  print a$ 
  end
 
 
DEL 

A separator other than the comma can be specified with str_expr2.

Example 6-59 ELEMENT$ function - separators

  let sentence$ = 'This is a test.' 
  let a$ = element$(sentence$,2,' ')
  print a$ 
  end
  
 
is 

More than one separator in a row returns a null for the corresponding element.

Note on the following example:

The following example shows how having two commas [the default separator] in a row will cause the result to be nothing [null] for that particular element.

Example 6-60 ELEMENT$ function - separators

  let sentence$ = 'This,, is, a, test'  
  print element$(sentence$, 2) 
  end
 
 
 

6.4.11 ENCODE$(num_expr, num_int)

The ENCODE$ function returns a string containing a number converted to the base specified. num_expr is the value to convert. num_int is the base to convert. For instance, '2' indicates binary, etc. See also Section 6.9.1, DECODE(str_expr, int_expr)

Example 6-61 ENCODE$ function

  do
    input 'Enter a number to convert to hex': decnum 
    if  decnum = 0 or _exit  then exit do
    print 'Hex for'; decnum;' is '; encode$(decnum,16) 
  loop
  end
  
  
Enter a number to convert to hex? 34.56 
Hex for 34.56 is 22 
Enter a number to convert to hex? 255 
Hex for 255 is FF 

6.4.12 FORMAT$(expr, str_expr)

Given an expression and a format, FORMAT$ returns the result of the expression in the format indicated.

The '@' format character causes the character not to be translated by the formatter. The '<' and '>' are treated like an '@' character. You can justify a character string, but avoid zero suppression and zero insertion.

The FORMAT$ function takes an expression of any data type for the data to format (the first argument), including string expressions.

Example 6-62 FORMAT$ function

  z$ = format$('1234567', '###~-####') 
  print 'Phone number: '; z$ 
  end
 
 
Phone number: 123-4567 

The FORMAT$ function returns all asterisks "*" in the case of overflow.

Example 6-63 FORMAT$ function

  z$ = format$(12.23,'#.##') 
  print z$ 
  end
 
 
**** 

FORMAT$() returns the same string data as given by the following:


        PRINT USING str_expr: expr 

The FORMAT$ function supports the DATE format and date arguments. Given a date in YYMMDD or CCYYMMDD format, FORMAT$ returns the date in the date format requested.


        FORMAT$(z$, '{DATE [argument]}?') 

The ? can be replaced with a mask. If no date argument is provided, the default is MDCY.

Example 6-64 DATE format with FORMAT$

  z1$ = format$('990122', '{date mdcy}?') 
  z2$ = format$('990122', '{date mdcy}##/##/####') 
  z3$ = format$('20000122', '{date mdcy}?') 
  z4$ = format$('20000122', '{date mdcy}##/##/####') 
  print z1$, z2$ 
  print z3$, z4$ 
  end
 
 
01221999            01/22/1999 
01222000            01/22/2000 

Table 6-7 FORMAT$ function - date arguments
DATE
Argument
YYMMDD
Input
Result CCYYMMDD
Input
Result
none 990207 02072000 20000207 02072000
YMD 990207 990207 20000207 990207
CYMD 990207 20000207 20000207 20000207
MDY 990207 022199 20000207 022199
MDCY 990207 02072000 20000207 02072000
DMY 990207 070299 20000207 070299
DMCY 990207 07022000 20000207 07022000
DMONY 990207 07-Feb-99 20000207 07-Feb-99
DMONCY 990207 07-Feb-2000 20000207 07-Feb-2000
MONTHDY 990207 February 7, 99 20000207 February 7, 99
MONTHDCY 990207 February 7, 2000 20000207 February 7, 2000


Previous Next Contents Index