Wednesday, July 12, 2017

REXX: Looping in REXX

A loop is a repetitive execution of a set of statements until some conditions are met.

Do I = 1 To 10
   Say "Value of I :" I
End

LEAVE verb leaves the immediate loop after execution.

Do I = 1 To 10 
   If I = 5 then LEAVE
   Say "Value of I :" I
End 

Result would be: 
Value of I : 1
Value of I : 2
Value of I : 3
Value of I : 4

ITERATE verb ignores the statement following it and starts the next iteration in the loop

Do I = 1 To 10 
   If I = 5 then ITERATE
   Say "Value of I :" I
End 

Result would be: 
Value of I : 1
Value of I : 2
Value of I : 3
Value of I : 4
Value of I : 6
.
.
.
Value of I : 10

Monday, July 10, 2017

REXX: Built in String Functions

String functions interrogate, compare, and manipulate character strings of data.

Some of the built-in function are listed below:

COPIES:
This function returns n copies of string concatenated together.

Syntax:    COPIES(string,n)

Examples:

COPIES('123',2)    -   /*   '123' */
COPIES('%',5)      -   /*   '%%%%%'   */

DELSTR:

This function returns length characters deleted from string, starting at n.

If length is not specified, all characters from n are deleted.

Syntax:  DELSTR(string, n, length)

Examples:

DELSTR('GreatIndia', 6)       /*Great*/
DELSTR('GreatIndia', 6,1)       /*Greatndia*/


INSERT:

This function will insert the characters new into target. All other parameters are optional. n specifies the location in target after which to insert new. The default for n is 0, which means to insert before the beginning of target.

Syntax:    INSERT(new, target, n, length, pad)

Examples:

INSERT('new','old')                 /*  'newold'             */
INSERT('new','old',2)              /*  'olnewd'             */
INSERT('new','old',2,5,'*')      /*  'olnew**d'         */
INSERT('new','old',7,4,'*')      /*  'old****new*'   */

OVERLAY:

This function will replace characters in target with characters from new. It will overlay target with new starting from position n in target.

If n is omitted, overlaying begins at position 1 in target. If length is specified, it is used to pad or truncate new.

If padding is required, the pad character specified by pad will be used. Otherwise, the default pad character of blank will be used.

Syntax: OVERLAY(new, target, n, length, pad)

Examples:

OVERLAY('abcd','1234efgh')                             /*   'abcdefgh'    */
OVERLAY('abcd','1234efgh',5)                          /*   '1234abcd'   */
OVERLAY('abcd','1234efgh',,6)                         /*   '1234 abcd'  */ 
OVERLAY('abcd','1234efgh',10,6,'*')                /*   '1234efgh*abcd**'   */

REVERSE:

This function returns a character string swapped end-over-end. The first character of string becomes the last and vice versa.

Syntax:   REVERSE(string)

Examples:

REVERSE('abcd')        returns 'dcba'
REVERSE('alla')          returns 'alla'


SUBSTR:

This function returns a substring of string starting at character n of length characters.

If length is not specified, the remainder of string is returned. If length is specified, it is used to truncate or pad the remainder of string.

The default pad character is a blank space.

SUBSTR('1234567',4)           returns '4567'
SUBSTR('1234567',4,2)        returns '45'
SUBSTR('1234567',6,3,'*')    returns '67*'

XRANGE:

This function returns a string of all one byte codes between and including a specified start and end character in a given string.

It can be useful in assigning entire alphabet to a variable.

Syntax:   XRANGE(start, end)

Examples:

XRANGE(a,d)       returns 'abcd'
XRANGE(A,D)     returns 'ABCD'

ARG:
This function can be used to determine the number of arguments passed, value of the argument and whether an argument exist or is omitted.

Syntax: ARG(n,option)

Examples:

Callrexx 'firstvalue'  ' '  'thirdvalue'

ARG()         returns 3
ARG(1)       returns 'firstvalue'
ARG(2)       returns ' '
ARG(3)       returns 'thirdvalue'
ARG(1,'e')   returns true

COMPARE:

This function compares two strings and returns 0 if they match. If strings are not equal, COMPARE returns the position of the first character that does not match.


COMPARE pads the shorter string with specified pad character, which is blank space by default.

Syntax:   COMPARE(string1, string2, pad)

Examples:

COMPARE('123','123')   returns 0   '123' = '123'
COMPARE('12 ','12')      returns 0   '12 '  = '12 '
COMPARE('12 ','12','-')  returns 3   '12 ' /= '12-'
COMPARE('12','123')     returns 3   '12'  /= '123'


LASTPOS:

This function returns the starting position of the last occurrence of the needle in haystack. If needle is not found in haystack, 0 is returned.

If start is not specified, the search begins at the end of haystack and continues left toward the beginning of haystack. If start is specified, searching commences at start and continues left toward the beginning of haystack.

Syntax: LASTPOS(needle,haystack,start)

Examples:

LASTPOS('23' , '12345 12345')    returns 8
LASTPOS('23' , '12345 12345',5) returns 2
LASTPOS('23' , '12345 12345',3) returns 0


LENGTH:

This function returns the length of the string passed to it.

Syntax: LENGTH(string)

Examples:

LENGTH('GreatIndia')    returns 10

DATATYPE:
This function is used to verify input data and can be used prevent errors. DATATYPE checks a string to the REXX definition of a string type. If only string specified, DATATYPE returns NUM if the string is a valid number. Otherwise, it returns CHAR.

Syntax:   DATATYPE(string,type)

Examples:

number = 10
IF DATATYPE(number) = 'NUM' then SAY 'valid number'
else SAY 'not a valid number, check the value in the variable number'

DATATYPE('aa1')   /* returns CHAR*/
DATATYPE(99)      /* returns NUM */


Type
Expansion
Description
A
Alphanumeric
Returns 1 if string contains only from range a-z,A-Z,0-9
B
Binary
Returns 1 if string contains only 0 or 1, or both
L
Lowercase
Returns 1 if string contains only from range a-z
M
Mixed
Returns 1 if string contains only from range a-z and A-Z
N
Number
Returns 1 if string is a valid number
S
Symbol
Returns 1 if string is a valid symbol
U
Uppercase
Returns 1 if string contains only from range A-Z
W
Whole
Returns 1 if string is a whole number
X
Hexadecimal
Returns 1 if string contains only from range a-z,A-Z,0-9 and blank when blanks appear only between pairs of hexadecimal characters.

POS:

This function returns the starting position of needle in haystack. If needle cannot be located in haystack, POS returns 0.

If start is not specified, the search commences at the start of haystack and continues right towards the end of haystack. If start is specified, searching commences at start and continues right towards the end of haystack.

The pos(needle, haystack) function is same as INDEX(haystack, needle) function.

Syntax:  POS(needle, haystack, start)

Examples:

POS('ab','abcdefg')    /*returns 1*/
POS('ab','abcdefg',2) /*returns 0 */
POS('x','abcdefg')     /*returns 0*/

SYMBOL:

This function interrogates the REXX variable pool to determine whether a variable has been set to a value.

It returns 'BAD' if the specified string is not a valid REXX symbol. 'VAR' if the string is a variable or 'LIT' for others.

Syntax:  SYMBOL(value)

Examples:

a='2'
SYMBOL('a')     Returns VAR
SYMBOL(a)       Returns LIT
SYMBOL('*')      Returns BAD

VERIFY:

This function verifies that a specified string only contains characters from a specified reference string by returning the position of the first character that is not in the reference, or 0 if the string is composed only of characters in the reference.

Also, this can determine the first character of the string that is in reference by using the MATCH option. A start position can also be defined.

Syntax: VERIFY(target, reference, option , start)

Examples:

VERIFY('I am hero','amhero')     Returns 1



Sunday, July 9, 2017

REXX: Built in Word Functions

A word is defined as a blank delimited set of characters within a string in REXX. The characters in a word can be alpha, numeric, alphanumeric, or hex, or binary or special characters as the REXX interpreter does not differentiate between them within a string.

Word functions are designed to process words within a string. 

Some of the word functions and their descriptions are given in the below table. 

Function
Description
SUBWORD
Returns a substring of a given string from a specified word for a specified number of words.
WORD
Returns the nth specified word in a string.
WORDINDEX
Returns the position of the first character in a specified word in a string.
DELWORD
Deletes one or more words, starting a specified word
WORDLENGTH
Returns the length of a specified word
WORDPOS
Returns the position of a specified word or phrase in a given string
WORDS
Returns the number of words in a given string
FIND
Returns the word number of the first occurrence of a given string in a phrase. WORDPOS is usually preferred.

WORDS:
This function returns the number of words in a string
Syntax: WORDS(string)

Examples: 

WORDS('I am a hero')    /* returns 4*/
WORDS('1 2 3 4 ')          /* returns 4*/
WORDS('! @ # $ ')          /* returns 4*/
WORDS('a b c d ')          /* returns 4*/

WORD:
This function returns the nth word from a string. 

Syntax: WORD(string,n)

Examples: 

WORD('(I am a hero',2)                /* returns 'am' */

Coding word(string,n) is the same as coding subword(string,n,1)

WORDPOS:

This function returns the word number of the first word of phrase from string. If the start is specified, it identifies the word in string at which to begin the search. If phrase is not found, WORDPOS returns 0. 

The FIND function could be used instead of WORDPOS, but the syntax is slightly different, for example:

FIND(string,phrase,start)

WORDPOS is preferred for standardization reasons. 

Syntax: WORDPOS(phrase,string,start)

Examples:

WORDPOS('am a','i am a hero')         /*returns 2*/
WORDPOS('am a','i am a hero',2)      /*returns 2*/
WORDPOS('am a','i am a hero',3)      /*returns 0*/

SUBWORD:

This function is similar to the SUBSTR function, except the n specifies the nth word of the string, and length specifies the number of words in the returned string. If length is not specified, it defaults to the rest of the words in the string. 

Specifying SUBWORD(string,4,1) is same as specifying with WORD function like WORD(string,4)

Syntax: SUBWORD(string, n, length) 

Examples:

SUBWORD('I am a hero',2)    /*returns 'am a hero'  */
SUBWORD('I am a hero',2,2) /*returns 'am a'         */ 

WORDINDEX:
This function returns character position of the first character of word n from string.

If there are fewer than n words in string, WORDINDEX is 0. 

Syntax: WORDINDEX(string, n)

Examples: 

WORDINDEX('i am a hero',2)    /*returns 3*/
WORDINDEX('i am a hero',4)    /*returns 8*/   

WORDLENGTH:

This function returns the length of word n from string

If there are fewer than n words in string, WORDLENGTH returns 0.

Syntax: WORDLENGTH(string,n)

Examples:  

WORDLENGTH('i am a hero',2)     /*returns 2*/

WORDLENGTH('i am a hero',2)     /*returns 4*/  

DELWORD:

This function returns length words deleted from the string, starting at word n.

If length is not specifed, all words from n are deleted.

Syntax: DELWORD(string,n, length)

Examples:

DELWORD('i am a hero',3)     /* returns ' i am ' */
DELWORD('i am a hero',3,1)  /* returns ' i am hero' */

REXX: Built in Text Functions

They are many type of functions available in REXX. In this post, we will get in details of Text Functions.

Text functions interrogate or manipulate specific text within a character string.


  • Abbrev - Used to test for the text and length of abbreviations. Returns either a 1 or 0. 
  • Space   - Format a given line with a specified number of pad characters between each word in the line. 
  • Translate - Translates characters to uppercase or other characters. 

ABBREV:

This function compares the first length characters of information to info and returns 1 (true) if they match exactly including case. Otherwise, 0(false) is returned.

Syntax:  ABBREV(information,info,length(optional)

Example:

ABBREV('TEST','TES')             /*returns 1*/
ABBREV('TEST','tes')               /*returns 0*/  
ABBREV('TEST','TE',3)           /*returns 0 */        
ABBREV('TEST','')                  /*returns 1*/

the default for length is the number of characters in info.

SPACE:

This function replaces all spaces in between each word in a string with the pad characters for the specified n value. Leading and trailing spaces are always removed. 

The default for n is 1 and the default for pad is space. 

Syntax: SPACE(string, n, pad)

Example:
SPACE(' I am a hero ')          /*'I am a hero'*/
SPACE('I am a hero ',2,'*')   /*'I**am**a**hero'*/
SPACE('I am a hero',0,'*')    /*'Iamahero*/ 
SPACE('I am a hero','','*')     /*'I*am*a*hero*/

TRANSLATE:

This functions does translate each character in the string according the character map that is defined. If no character mapping is done, then only the string is translated to uppercase. 

Syntax: TRANSLATE(string,replace string, search string, pad)

Example:
TRANSLATE("AbendMaker")              /*returns 'ABENDMAKER'  */
TRANSLATE("Abend",'1','A')               /*returns '1bend'   */
TRANSLATE("Abend",'1','Ab')             /*returns '1 end'    */

TRANSLATE("Abend",'1','Ab','*')        /*returns '1*end'   */
TRANSLATE("Abend",'abcdefghijklmnopqrstuvwxyz','ABCDEFGHIJKLMNOPQRSTUVWXYZ')                                                                /*returns 'abend'   */
TRANSLATE("Abend",xrange('a','z'),xrange('A','Z')        /*returns 'abend'   */

If only input string is provided, all the alpha characters would be converted to Uppercase.
TRANSLATE converts only the string to uppercase and their is no lowercase conversion option. In order to convert to lowercase, the last two examples shall be used using the TRANSLATE function. 

When the replace string has fewer characters than the search string, the later characters of search string will be replaced with spaces or if specified with pad characters. 

Featured Post

REXX Skeleton: Submitting jobs through Rexx

Submitting jobs through REXX and reading the spool through REXX gives us immense potential to automate many manual activities in mainframes....