Rexx – Parsing ”; Previous Next One of the most powerful features of Rexx is its ability to parse text values. You probably will not see this in any other programming languages. The general format of the parse statement is as follows − Syntax PARSE {UPPER|LOWER|CASELESS} source {template} Where, UPPER − The source is converted to upper case before parsing. LOWER − The source is converted to lower case before parsing. CASELESS − When this parameter is passed, the casing is ignored. source − This is the source which needs to be parsed. There are many options available for this and can be any one of the following − ARG − The arguments for the program or procedure can be used as the source. LINEIN − The next line input can be used as the source. SOURCE − The source information of the program can be used as the source. VAR name − The value of a variable name can be used as the source. template − This parameter specifies how to parse the source. There are many options available for this. Some of them are mentioned below. variable name − This is the value assigned to the variable. literal string − A literal string which can be used a pattern to split the strung. # − An absolute character position within the source itself. So if you specify a value of 5, the 5th character will be used. +# − A relative character position within the source itself. So if you specify a value of 5, the 5th character will be used relatively. Let’s look at a simple example of how parsing can be accomplished in Rexx. Example Live Demo /* Main program */ parse value ”This is a Tutorial” with word1 word2 word3 word4 say “””word1″”” say “””word2″”” say “””word3″”” say “””word4″”” The above program parses the words in the phrase. When a value consists of words that are separated by only one space, and there are no leading or trailing spaces, the value is easy to parse into a known number of words as follows. The parse function is used in Rexx to take a string value and then break them down into words. In the above example, the words are then split and then stored in the word variables. The output of the above program would be as follows − ”This” ”is” ”a” ”Tutorial” Another example of parsing is shown in the following program. This time we are using a while clause to do the parsing. Example Live Demo /* Main program */ phrase = ”This is a Tutorial” do while phrase <> ”” parse var phrase word phrase say “””word””” end The above program will give the following output − ”This” ”is” ”a” ”Tutorial” Positional Parsing Rexx also allows one to work with positional parsing. Let’s see an example of how we can achieve positional parsing with the parse statement. Example Live Demo /* Main program */ testString = “Doe John M. 03/03/78 Mumbai India”; parse var testString name1 11 name2 21 birthday 31 town 51 country say name1 say name2 say birthday say town say country From the above example, you can note that along with the variable name, we are also specifying where the string should end. So for name1, we should end by the 11th character and then starting parsing name2. The output of the above program will be as follows − Doe John M. 03/03/78 Mumbai India You can also use relative positional parsing in this case. Example Live Demo /* Main program */ testString = “Doe John M. 03/03/78 Mumbai India”; parse var testString name1 +10 name2 +10 birthday +10 town +20 country say name1 say name2 say birthday say town say country The output of the above program will be as shown below. Doe John M. 03/03/78 Mumbai India Print Page Previous Next Advertisements ”;
Category: rexx
Rexx – System Commands
Rexx – System Commands ”; Previous Next One of the biggest advantages in Rexx is the ability to create re-usable scripts. Often in organizations nowadays, having re-usable scripts is a big value add in saving time to do common repetitive tasks. For example, technology teams in an IT organization can have the need to have scripts which do common everyday tasks. These tasks can include interacting with the operating systems. These scripts can then be programmed to handle bad return codes or errors. Rexx offers a lot of system commands that can be used to perform such repetitive tasks. Let’s look at some of the system commands available in Rexx. dir This is the normal directory listing command which is used in Windows. Syntax dir Parameters None Return Value This method returns the current directory listing on the system. Example /* Main program */ dir The output depends on the directory in the system. The following program is just an example. Output Volume in drive D is LENOVO Volume Serial Number is BAC9-9E3F Directory of D: 04/06/2016 12:52 AM 268,205 100008676689.pdf 10/20/2015 08:51 PM <DIR> data 06/01/2016 10:23 AM 31 Example.txt 10/28/2014 06:55 PM <DIR> Intel 06/02/2016 11:15 AM 23 main.rexx 12/22/2014 08:49 AM <DIR> PerfLogs 12/13/2015 11:45 PM <DIR> Program Files 12/24/2015 10:26 AM <DIR> Program Files (x86) 07/17/2015 01:21 AM <DIR> Users 12/23/2015 10:01 AM <DIR> Windows 3 File(s) 268,259 bytes 7 Dir(s) 202,567,680 bytes free Another example of the dir command is shown in the following program. Only this time we are making use of the special rc variable. This variable is special in Rexx and gives you the status of the execution of system commands. If the value returned is 0, then that means the command is executed successfully. Else the error number will be given in the rc variable name. Example Live Demo /* Main program */ dir if rc = 0 then say ”The command executed successfully” else say ”The command failed, The error code is =” rc When we run the above program we will get the following result. Output The command failed, The error code is = 127 Redirection Commands Rexx also has the facility of using redirection commands. The following redirection commands are available in Rexx. < − This command is used to take in the input which comes from a file. > − This command is used to output the content to a file. If the file does exist, it will be over-written. >> − This is also used to output the content to a file. But the output is added to the end of the file to preserve the existing content of the file. Let’s look at an example of how we can use redirection commands. In the following example, we are using the sort command to sort a file called sortin.txt. The data from the file is sent to the sort command. The output of the sort command is then sent to the sortout.txt file. Example /* Main program */ ”sort <sortin.txt> sortout.txt” Assume that the file sortin.txt has the following data. Output b c a The file sortout.txt will then have the following data. a b c The ADDRESS Function This method is used to find out what is the default environment used for the Input, Error and Output streams. Syntax ADDRESS(options) Parameters Options for what is the address of a particular system. Return Value This method returns the name of the environment for the Input, Error and Output streams. Example /* Main program */ say ADDRESS(”I”) say ADDRESS(”O”) say ADDRESS(”E”) When we run the above program we will get the following result. Output INPUT NORMAL REPLACE NORMAL REPLACE NORMAL Print Page Previous Next Advertisements ”;
Rexx – Signals
Rexx – Signals ”; Previous Next In Rexx, the signal instruction is used generally for two purposes, which are − One is to transfer control to another part of the program. This is normally like the go-to label which is used in other programming languages. The other is to go to a specific trap label. If the signal command is used in any of the following instruction commands, the pending control structures will automatically be deactivated. if … then … else … do … end do i = 1 to n … end [and similar do loops] select when … then … …etc. otherwise … end The general syntax of the signal statement is shown as follows − Syntax signal labelName signal [ VALUE ] labelExpression Let’s look at an example of how to use the signal statement. Example Live Demo /* Main program */ n = 100.45 if datatype( n, wholenumber ) then signal msg say ”This is a whole number” return 0 msg : say ”This is an incorrect number” The output of the above program will be as shown below. Output This is an incorrect number. If you change the value of the variable n to a whole number as shown in the following program − Live Demo /* Main program */ n = 100 if datatype( n, wholenumber ) then signal msg say ” This is a whole number ” return 0 msg : say ” This is an incorrect number ” You will get the following output − This is a whole number One can also transfer to the value of the label as shown in the following program − Live Demo /* Main program */ n = 1 if datatype( n, wholenumber ) then signal msg if n < 1 | n > 3 then signal msg signal value n 3 : say ”This is the number 3” 2 : say ” This is the number 2” 1 : say ” This is the number 1” return n msg : say ” This is an incorrect number ” exit 99 The output of the above program will be shown as follows − This is the number 1 Trap Label Transfer Activation / Deactivation As we have mentioned earlier, the signal instruction can also be used to transfer control to a trap label. The general syntax of the Trap label transfer is given as follows − Syntax signal ON conditionName [ NAME Label ] signal OFF conditionName Where, conditionName − This is the condition for which the signal should be either be turned on or off. Label − The optional label to which the program should be diverted to. Let’s see an example of using a trap label transfer. Example /* Main program */ signal on error signal on failure signal on syntax signal on novalue beep(1) signal off error signal off failure signal off syntax signal off novalue exit 0 error: failure: syntax: novalue: say ”An error has occured” In the above example, we first turn the error signals on. We then add a statement which will result in an error. We then have the error trap label to display a custom error message. The output of the above program will be as shown follows − An error has occurred. Print Page Previous Next Advertisements ”;
Rexx – Regina
Rexx – Regina ”; Previous Next Regina is another Rexx interpreter available to compile and run Rexx programs. The official site for Regina is − www.regina-rexx.sourceforge.net/ Some of the advantages of using Regina are as follows − Regina can run on any platform whether it be Windows, Linux or the Mac OS. Regina works as per all available standards. Regina has a big community following and hence there are a lot of forums and learning material available for Regina. Regina has a lot of tools available for writing and testing Rexx programs. In Regina, you can run commands which are not possible in the default Rexx Interpreter. For example, if you include certain configuration settings, you can actually run basic system level commands, which is not possible in Rexx. When you install Rexx via the installation documented in Chapter 2 – Rexx Environment, the Regina interpreter gets installed along with it. Now let’s see some of the common methods available when using Regina. These functions are the extended functions which are not available via normal use. To make use of the extended functions, you need to include the following line of code. This enables the use of Regina extended functions. options arexx_bifs Secondly while running all Rexx programs, use the following command. regina main.rexx Where, regina − This is the interpreter used for Rexx programs. main.rexx − Your Rexx program. We will now discuss in detail the various functions of Regina Rexx Interpreter. Sr.No. Functions of Regina Rexx Interpreter 1 b2c This method is used to convert a binary value to a string value. 2 bitcomp The method is used to compare 2 bit strings, bit by bit. 3 bittst This method is used to indicate the state of the specified bit in the bit string. 4 find This method is used to search for the first occurrence of a string in another string. 5 getenv This method returns the value of an environment variable on the system. 6 getpid This method is used to get the value of the current running process id. 7 hash This method returns the hash attribute of a string as a decimal number. It also updates the internal hash value of the string. 8 justify This method is used to add justify or trim the value of a string based on the length value. 9 putenv This method is used to set the value of an environment variable. 10 directory This method gets the value of the current directory on the system. 11 chdir This method changes the value of the current working directory on the system. 12 randu This method returns a pseudo-random number between 0 and 1. Print Page Previous Next Advertisements ”;
Rexx – XML
Rexx – XML ”; Previous Next XML is a portable, open source language that allows programmers to develop applications that can be read by other applications, regardless of the operating system and/or developmental language. This is one of the most common languages used for exchanging data between applications. What is XML? The Extensible Markup Language XML is a markup language much like HTML or SGML. This is recommended by the World Wide Web Consortium and available as an open standard. XML is extremely useful for keeping track of small to medium amounts of data without requiring a SQL-based backbone. For all our XML code examples, let”s use the following simple XML file movies.xml for construction of the XML file and reading the file subsequently. <collection shelf = “New Arrivals”> <movie title = “Enemy Behind”> <type>War, Thriller</type> <format>DVD</format> <year>2003</year> <rating>PG</rating> <stars>10</stars> <description>Talk about a US-Japan war</description> </movie> <movie title = “Transformers”> <type>Anime, Science Fiction</type> <format>DVD</format> <year>1989</year> <rating>R</rating> <stars>8</stars> <description>A schientific fiction</description> </movie> <movie title = “Trigun”> <type>Anime, Action</type> <format>DVD</format> <year>1986</year> <rating>PG</rating> <stars>10</stars> <description>Vash the Stam pede!</description> </movie> <movie title = “Ishtar”> <type>Comedy</type> <format>VHS</format> <year>1987</year> <rating>PG</rating> <stars>2</stars> <description>Viewable boredom </description> </movie> </collection> Getting Started By default, the xml functionality is not included in the Rexx interpreter. In order to work with XML in Rexx, the following steps need to be followed. Download the following files − Rexxxml − www.interlog.com/~ptjm/ Libxml2 − www.ctindustries.net/libxml/ iconv-1.9.2.win32 − www.xmlsoft.org/sources/win32/oldreleases/ libxslt-1.1.26.win32 − www.xmlsoft.org/sources/win32/oldreleases/ Extract all of the files and ensure they are included in the system path. Loading XML Functions Once all the files in the above section have been downloaded and successfully registered, the next step is to write the code to load the Rexx XML functions. This is done with the following code. rcc = rxfuncadd(”XMLLoadFuncs”, ”rexxxml”, ”xmlloadfuncs”) if rcc then do say rxfuncerrmsg() exit 1 end call xmlloadfuncs The following things can be noted about the above program − The function rxfuncadd is used to load external libraries. The xmlloadfuncs function is used to load all the libraries in the rexxxml file into memory. If the value of rcc<>0, then it would result in an error. For this , we can call the rxfuncerrmsg to give us more details on the error message. We finally make a call to xmlloadfuncs, so that all xml related functionality can now be enabled in the Rexx program. Let’s look at the various methods available for XML in Rexx. xmlVersion This method returns the version of the XML and XSLT libraries used on the system. Syntax xmlVersion() Parameters None Return Value This method returns the version of the XML and XSLT libraries used on the system. Example rcc = rxfuncadd(”XMLLoadFuncs”, ”rexxxml”, ”xmlloadfuncs”) if rcc then do say rxfuncerrmsg() exit 1 end call xmlloadfuncs say xmlVersion() When we run above program we will get the following result. This again depends on the version of the XML libraries being used on the system. Output 1.0.0 20631 10126 xmlParseXML This function is used to parse the XML data sent to the function. The document tree is returned by the function. Syntax xmlParseXML(filename) Parameters Filename − This is the name of the XML file which needs to be parsed. Return Value The document tree is returned by the function. Else returns 0, if there is an error. Example rcc = rxfuncadd(”XMLLoadFuncs”, ”rexxxml”, ”xmlloadfuncs”) if rcc then do say rxfuncerrmsg() exit 1 end call xmlloadfuncs say xmlVersion() sw = xmlParseXML(”test.xml”) Output No general output. xmlFindNode This method evaluates the XPath expression passed to it. This is used for parsing the document tree to result a nodeset which can be processed further. Syntax xmlParseXML(XPath,document) Parameters XPath − This is the path of the node in the xml file. document − This the XML document Return Value Evaluates XPath expression and returns result as a nodeset which can be used later on. Example rcc = rxfuncadd(”XMLLoadFuncs”, ”rexxxml”, ”xmlloadfuncs”) if rcc then do say rxfuncerrmsg() exit 1 end call xmlloadfuncs say xmlVersion() document = xmlParseXML(”test.xml”) nodeset = xmlFindNode(”//movie”, document) say xmlNodesetCount(nodeset) When we run above program we will get the following result. Output 4 The output shows the number of movie nodes in our xml list xmlEvalExpression The below method is used to Evaluate an XPath expression and return a string as a result. Syntax xmlParseXML(XPath,Node) Parameters XPath − This is the path of the node in the xml file. document − The specific node element. Return Value A string is returned based on the XPath expression sent to it. Example rcc = rxfuncadd(”XMLLoadFuncs”, ”rexxxml”, ”xmlloadfuncs”) if rcc then do say rxfuncerrmsg() exit 1 end call xmlloadfuncs document = xmlParseXML(”test.xml”) nodeset = xmlFindNode(”//movie”, document) do j = 1 to xmlNodesetCount(nodeset) value = xmlEvalExpression(”type”, xmlNodesetItem(nodeset, j)) say value end When we run above program we will get the following result. Output War, Thriller Anime, Science Fiction Anime, Action Comedy Print Page Previous Next Advertisements ”;
Rexx – Subroutines
Rexx – Subroutines ”; Previous Next In any programming language, the entire program is broken into logical modules. This makes it easier to write code that can be maintained easily. This is a basic requirement for any programming language. In Rexx, modules can be written using Subroutines and functions. Let’s look at the subroutines in detail. Defining a Subroutine The syntax of a function declaration is as follows − FunctionName: Statement#1 Statement#2 …. Statement#N Where, FunctionName − This is the name assigned to the subroutine. Statement#1 .. Statement#N − These are the list of statements that make up the subroutine. The following program is a simple example showing the use of subroutines. Live Demo /* Main program */ call add exit add: a = 5 b = 10 c = a + b say c The following things should be noted about the above program − We are defining a subroutine called add. The subroutine does a simple add functionality. The exit statement has to be used to signify the end of the main program. The output of the above program would be as follows − 15 Working with Arguments It is also possible to work with arguments in Rexx. The following example shows how this can be achieved. Live Demo /* Main program */ call add 1,2 exit add: PARSE ARG a,b c = a + b say c The following things should be noted about the above program − We are defining a subroutine called add which takes on 2 parameters. In the subroutines, the 2 parameters are parsed using the PARSE and ARG keyword. The output of the above program would be as follows − 3 Different Methods for Arguments Let’s look at some other methods available for arguments. arg This method is used to return the number of arguments defined for the subroutine. Syntax − arg() Parameters − None Return Value − This method returns the number of arguments defined for the subroutine. Example − Live Demo /* Main program */ call add 1,2 exit add: PARSE ARG a,b say arg() c = a + b say c When we run the above program we will get the following result. 2 3 arg(index) This method is used to return the value of the argument at the specific position. Syntax − arg(index) Parameters Index − Index position of the argument to be returned. Return Value − This method returns the value of the argument at the specific position. Example − Live Demo /* Main program */ call add 1,2 exit add: PARSE ARG a,b say arg(1) c = a + b say c When we run the above program we will get the following result. 1 3 Print Page Previous Next Advertisements ”;
Rexx – Built-In Functions
Rexx – Built-In Functions ”; Previous Next Every programming language has some built-in functions that help the programmer do some routine tasks. Rexx also has a lot of built in functions. Let’s look at all of these functions available in Rexx. Sr.No. Functions available in Rexx 1 ADDRESS This method returns the name of the environment in the which the Rexx commands are currently running in. 2 BEEP This method produces a sound in the system at a particular frequency and duration. 3 DataType This method returns the value of ‘NUM’ if the input is a valid number else it will return the value of ‘CHAR’. You can also specify if you want to compare the input value to a NUM or CHAR value. In each case, the value returned will be either 1 or 0 depending on the result. 4 DATE This method returns the local date in the following format. 5 DIGITS This method returns the current setting of NUMERIC DIGITS as defined in the current system. 6 ERRORTEXT This method returns the Rexx error message associated with error number ‘errorno’. Please note that the error number needs to be a value from 0 to 99. This is useful in cases wherein your program returned an error code and you want to know what the error code means. 7 FORM This method returns the current setting of ‘NUMERIC FORM’ which is used to do mathematic calculations on the system. 8 TIME This method returns the local time in the 24-hour clock format as shown in the following program. 9 USERID This method returns the current user id logged into the system. 10 XRANGE This method returns the characters in the range specified by the start and end character. 11 X2D This method returns the decimal conversion of a hexstring value. 12 X2C This method returns the character conversion of a hexstring value. Print Page Previous Next Advertisements ”;
Rexx – File I/O
Rexx – File I/O ”; Previous Next Rexx provides a number of methods when working with I/O. Rexx provides easier classes to provide the following functionalities for files. Reading files Writing to files Seeing whether a file is a file or directory The functions available in Rexx for File I/O are based on both line input and character input and we will be looking at the functions available for both in detail. Let’s explore some of the file operations Rexx has to offer. For the purposes of these examples, we are going to assume that there is a file called NewFile.txt which contains the following lines of text − Example1 Example2 Example3 This file will be used for the read and write operations in the following examples. Here we will discuss regarding how to read the contents on a file in different ways. Reading the Contents of a File a Line at a Time The general operations on files are carried out by using the methods available in the Rexx library itself. The reading of files is the simplest of all operations in Rexx. Let’s look at the function used to accomplish this. linein This method returns a line from the text file. The text file is the filename provided as the input parameter to the function. Syntax − linein(filename) Parameter − filename − This is the name of the file from where the line needs to be read. Return Value − This method returns one line of the file at a time. Example − /* Main program */ line_str = linein(Example.txt) say line_str The above code is pretty simple in the fact that the Example.txt file name is provided to the linein function. This function then reads a line of text and provides the result to the variable line_str. Output − When we run the above program we will get the following result. Example1 Reading the Contents of a File at One Time In Rexx, reading all the contents of a file can be achieved with the help of the while statement. The while statement will read each line, one by one till the end of the file is reached. An example on how this can be achieved is shown below. /* Main program */ do while lines(Example.txt) > 0 line_str = linein(Example.txt) say line_str end In the above program, the following things need to be noted − The lines function reads the Example.txt file. The while function is used to check if further lines exist in the Example.txt file. For each line read from the file, the line_str variable holds the value of the current line. This is then sent to the console as output. Output − When we run the above program we will get the following result. Example1 Example2 Example3 Writing Contents to a File Just like reading of files, Rexx also has the ability to write to files. Let’s look at the function which is used to accomplish this. lineout This method writes a line to a file. The file to which the line needs to be written to is provided as the parameter to the lineout statement. Syntax − lineout(filename) Parameter − filename − This is the name of the file from where the line needs to be written to. Return Value − This method returns the status of the lineout function. The value returned is 0 if the line was successfully written else the value of 1 will be returned. Example − /* Main program */ out = lineout(Example.txt,”Example4″) Output − Whenever the above code is run, the line “Example4” will be written to the file Example.txt. Print Page Previous Next Advertisements ”;
Rexx – Functions For Files
Rexx – Functions For Files ”; Previous Next In this chapter, we will discuss regarding some of the other functions that are available for files. Sr.No. Functions for Files 1 Lines This function returns either the value 1 or the number of lines left to read in an input stream. The filename is given as the input to the function. 2 stream This function is used to check the status of a file. Sometimes it is required to check the status of a file before it is used. If the file is corrupt or not available, then no further operations can be performed on the file. So it makes more sense to first check the status of the file. 3 charin This function is used to read one character at a time from a file. Sometimes programs require to read files character wise and hence this function can be used for this purpose. 4 chars This function returns either 1 or the number of characters left to read in the file itself. The filename is mentioned as a parameter to the function. 5 charout This function is used to write one character at a time to a file. The filename is entered as a parameter to the function. Print Page Previous Next Advertisements ”;
Rexx – Numbers
Rexx – Numbers ”; Previous Next Rexx has the following data types when it comes to numbers. Integer − A string of numerics that does not contain a decimal point or exponent identifier. The first character can be a plus (+) or minus (-) sign. The number that is represented must be between -2147483648 and 2147483647, inclusive. Big Integer − A string of numbers that does not contain a decimal point or an exponent identifier. The first character can be a plus (+) or minus (-) sign. The number that is represented must be between -9223372036854775808 and 2147483648, inclusive, or between 2147483648 and 9223372036854775807. Decimal − One of the following formats − A string of numerics that contains a decimal point but no exponent identifier, where p represents the precision and s represents the scale of the decimal number that the string represents. The first character can be a plus (+) or minus (-) sign. A string of numerics that does not contain a decimal point or an exponent identifier. The first character can be a plus (+) or minus (-) sign. The number that is represented is less than -9223372036854775808 or greater than 9223372036854775807. Float − A string that represents a number in scientific notation. The string consists of a series of numerics followed by an exponent identifier (an E or e followed by an optional plus (+) or minus (-) sign and a series of numerics). The string can begin with a plus (+) or minus (-) sign. Let’s now look at the different methods available for numbers. Sr.No. Methods available for Numbers 1 ABS This method returns the absolute value of an input number. 2 MAX This method returns the maximum value from a list of numbers. 3 MIN This method returns the minimum value from a list of numbers. 4 RANDOM This method returns a random generated number. 5 SIGN Returns 1 if number is greater than 0, or 0 if the number is 0, or -1 if the number is less than 0. 6 TRUNC This method truncates a number. Print Page Previous Next Advertisements ”;