23
Mon, Dec
1 New Articles

The CL Corner: A CL Command to Scan for a String of Characters

CL
Typography
  • Smaller Small Medium Big Bigger
  • Default Helvetica Segoe Georgia Times

Find a character pattern within a variable.

 

Last month, in "A CL Command to Scan for Characters," we built the command Scan Characters (SCNCHR). The SCNCHR command returns the position of the first character within a CL variable that is found in a list of search-for values. This month, we'll look at how to provide a Scan String (SCNSTR) command.

SCNSTR will scan a CL character variable to determine if a given pattern of characters, such as the pattern 'bag' is contained within the variable. If the pattern is found, the command will return the position of the first byte of the pattern within the CL variable; otherwise, a value of 0 will be returned. For instance, scanning for the pattern 'bag' within a variable set to the value of 'a cabbage' would return a value of 6, indicating that the pattern 'bag' is found starting at the sixth byte of the CL variable. This is in contrast to last month's SCNCHR command, where a scan of characters 'bag' within a CL variable would return a value of 1, indicating that the first character (the 'a') of 'a cabbage' is found within the list of searched-for values ('b', 'a', and 'g'). If you're familiar with the RPG %scan built-in, you'll notice that the SCNSTR command performs a very similar function.

Some of you may already be familiar with the system API that will be used to implement the SCNSTR command. It's the Scan for String Pattern (QCLSCAN) API, which is one of the few APIs that were available all the way back in the initial release of the AS/400 operating system OS/400. Its age however does not, in general, detract from its usefulness, and providing a command interface to QCLSCAN is likely to simplify the development efforts of your CL-based programming staff. You may note that I used the qualifier "in general" when referring to the age of the API. There are in fact some API restrictions—for instance, a maximum CL variable length of 999 characters to scan, which could prove to be a limitation when working with larger CL variables. With that in mind, next month we'll extend the capabilities of the SCNSTR command processing program (CPP) beyond some of the limitations found in the underlying API (though the SCNSTR command will still be limited to a maximum character variable length of 5000 bytes due to a limitation of the PARM command used when defining character-based command parameters).

The SCNSTR Command

As we did last month, we'll start by providing a command definition for the SCNSTR command. Note that this version of the command defines the parameter to scan (keyword Base) with a maximum length of 999 bytes to match the QCLSCAN API limitation. As mentioned previously, a future version of SCNSTR will support a maximum length of 5000 bytes.

Cmd       Prompt('Scan for String')                    

Parm       Kwd(ScanFor)   Type(*Char) Len(999) +      

             Min(1) Vary(*Yes *Int4) +                  

            Prompt('Char pattern to search for')      

Parm       Kwd(Base)       Type(*Char) Len(999) +      

             Min(1) Vary(*Yes *Int4) +                  

             Prompt('String to be scanned')            

Parm       Kwd(Pos)       Type(*UInt4) +

             Min(1) RtnVal(*Yes) +                            

             Prompt('Position of matching string')      

Parm       Kwd(StrPos)     Type(*UInt4) +

             Dft(1) Rel(*GT 0) +                              

             Prompt('Starting pos. within Base')        

Assuming that the previous command source is stored in member SCNSTR of source file QCMDSRC, you can create SCNSTR with the following command:

CrtCmd Cmd(ScnStr) Pgm(ScnStrCPP) Allow(*BPgm *IPgm)

This command indicates that the CPP of SCNSTR is program SCNSTRCPP and that the command can be run within a program. As with the previous CHKCHR and SCNCHR commands, you will not be able to run the SCNSTR command interactively from a command line due to the Pos parameter being a return value.

Below is the source for our CPP ScnStrCPP.

The SCNSTRCPP Command Processing Program

Pgm       Parm(&ScanFor_In &Base_In &Pos &StrPos)      

Dcl       Var(&ScanFor_In) Type(*Char) Len(1003)      

Dcl       Var(&ScanForLen) Type(*Int) Len(4) +      

             Stg(*Defined) DefVar(&ScanFor_In)        

Dcl       Var(&ScanFor)   Type(*Char) Len(999) +    

             Stg(*Defined) DefVar(&ScanFor_In 5)      

Dcl       Var(&Base_In)     Type(*Char) Len(1003)      

Dcl       Var(&BaseLen)     Type(*Int) Len(4) +      

             Stg(*Defined) DefVar(&Base_In)          

Dcl       Var(&Base)       Type(*Char) Len(999) +    

             Stg(*Defined) DefVar(&Base_In 5)        

Dcl       Var(&Pos)         Type(*UInt)                

Dcl       Var(&StrPos)     Type(*UInt)                

                                                        

Dcl       Var(&AdjBaseLen) Type(*Dec) Len(3 0)        

Dcl       Var(&AdjScnFLen) Type(*Dec) Len(3 0)        

Dcl       Var(&AdjStrPos) Type(*Dec) Len(3 0)            

Dcl       Var(&AdjPos)     Type(*Dec) Len(3 0)            

                                                            

Dcl       Var(&MsgDta)     Type(*Char) Len(8)              

Dcl       Var(&MsgDta1)   Type(*UInt) +                  

             Stg(*Defined) DefVar(&MsgDta 1)              

Dcl       Var(&MsgDta2)   Type(*UInt) +                  

             Stg(*Defined) DefVar(&MsgDta 5)              

                                                            

ChgVar     Var(&AdjScnFLen) Value(&ScanForLen)              

ChgVar     Var(&AdjBaseLen) Value(&BaseLen)                

ChgVar     Var(&AdjStrPos) Value(&StrPos)                  

                                                            

Call       Pgm(QCLSCAN) Parm(&Base &AdjBaseLen &AdjStrPos +

             &ScanFor &AdjScnFLen ' ' ' ' ' ' &AdjPos)      

                                                            

Select                                                      

   When   Cond(&AdjPos = -1) Then(Do)                

           ChgVar Var(&MsgDta1) Value(&ScanForLen)    

           ChgVar Var(&MsgDta2) Value(&BaseLen)        

           SndPgmMsg MsgID(ESC0004) MsgF(OurMsgs) +    

             MsgDta(&MsgDta) MsgType(*Escape)          

           EndDo                                      

   When   Cond(&AdjPos = -5) Then(Do)                

           ChgVar Var(&MsgDta1) Value(&StrPos)        

          ChgVar Var(&MsgDta2) Value(&BaseLen)        

           SndPgmMsg MsgID(ESC0003) MsgF(OurMsgs) +    

             MsgDta(&MsgDta) MsgType(*Escape)          

           EndDo                                      

   Otherwise +                                          

           Cmd(ChgVar Var(&Pos) Value(&AdjPos))        

EndSelect                                              

                                                      

EndPgm                                                

Assuming that the preceding source is stored in member SCNSTRCPP of source file QCLSRC, you can use the following command to create the SCNSTRCPP program:

CrtBndCL Pgm(ScnStrCPP)

In an error situation, the SCNSTRCPP program will send one of two escape messages. The first escape message, ESC0003STRPOS value of &1 is longer than the length of BASE value (&2)was created in last month's article, "A CL Command to Scan for Characters," in support of the SCNCHR command. This message is re-used by SCNSTRCPP. The second message, ESC0004SCANFOR length of &1 is longer than BASE length of &2is new and needs to be created. To add this message to the message file OURMSGS (a message file we've used in past articles), you can use the following command.

AddMsgD MsgID(ESC0004) MsgF(OurMsgs) +

Msg('SCANFOR length of &1 is longer than BASE length of &2') +

Fmt((*UBin 4) (*UBin 4))

How SCNSTRCPP Works

As with the previous articles, if you are not interested in how the SCNSTR command works, you can skip to the topic "Testing the SCNSTR command."

Before looking at the flow of SCNSTRCPP, we'll first get familiar with the QCLSCAN API. QCLSCAN defines nine parameters. The first eight parameters are inputs that you provide to the API. The ninth parameter is an output from the API that identifies either where the SCANFOR pattern was located within the BASE searched string or the type of error that was encountered by the API.

The first parameter is a variable-length character field that contains the string to be scanned. This string can have a length from 1 through 999 bytes.

The second parameter defines the length of the character string found in the first parameter. Unlike most system APIs, where length information is provided as 4-byte integer values, QCLSCAN defines this parameter as packed decimal with a length of three digits and zero decimal positions. The use of this definition is due to the API predating current system API standards.

The third parameter defines the position, within the character string found in the first parameter, where the scan is to start. This value, like the second parameter, is defined a packed decimal with a length of 3,0. The value of this parameter must be greater than 0 and not greater than the value of the second parameter (the length of the character string to scan).

The fourth parameter is a variable-length character field that contains the string, or pattern, that is to be scanned for. This string can have a length from 1 through 999 bytes.

The fifth parameter defines the length of the character string being scanned for. This value is defined as packed decimal with a length of 3,0. The value must be greater than 0 and not greater than the effective length of the string to scan. Note the qualifier "effective length" in the previous sentence. If the second parameter indicates that the string to scan is 100 bytes but the third parameter specifies that the scan is to start at the 97th byte, then the effective length of the string is only four bytes (character positions 97 through 100). Specifying a scanned-for length of five bytes when calling the API would be an error situation.

The sixth parameter indicates whether the API should convert any lowercase characters found in the first parameter to uppercase prior to scanning for the string found in the fourth parameter. The parameter is defined as a 1-byte character field where a value of '1' indicates that the string found in parameter 1 should be uppercased. Any other input value indicates that no uppercasing is to be performed. This uppercasing capability of the API is not used by the program SCNSTRCPP.

The seventh parameter indicates whether the API should trim trailing blanks from the end of the character string found in the fourth parameter. The parameter is defined as a 1-byte character field where a value of '1' indicates that trailing blanks are to be trimmed. Any other input value indicates that no trimming is to be performed. This trimming capability of the API is not used by the program SCNSTRCPP, in part due to the command analyzer already providing the trimmed length of the fourth parameter in variable &ScanForLen.

The eighth parameter indicates whether the API should treat any character in certain positions of the fourth parameter as matches. The parameter is defined as a 1-byte character field where any non-blank value represents a wildcard position. That is, a value such as '*' for the eight parameter, along with a fourth parameter scan-for value of 'b*g', indicates that the character 'b', followed by any character, and then followed by the character 'g' should be treated as a match when scanning the first parameter.

The ninth parameter is the sole return value from the API. The parameter is defined as a packed decimal field with a length of 3,0. If the returned value is 0, then no match was found in the first parameter (from the specified starting position, parameter three, for the scan-for value specified by the fourth parameter). If the returned value is greater than 0, then a match was found in the first parameter, with the returned value representing where the first character of the scanned-for value can be found within the first parameter. Note that this positive value is relative to the full length of the first parameter and not based on the starting relative location (the value of the third parameter). If the returned value is negative, then the API detected an error situation.

When the API detects an error, one of five possible negative values can be returned: negative one through negative five. Because the SCNSTRCPP program is not using all of the capabilities of QCLSCAN (no uppercasing, no checking for trailing blanks, and no wildcard scan character), only two of these five errors might be returned to our CPP. The two errors that could be returned are negative one (the scan-for pattern is longer than the string to scan) and negative five (the starting position is not valid).

With that review of the API out of the way, let's now look at the flow of the SCNSTRCPP program, which isn't much!

  1. As the API expects the numeric inputs for parameters 2, 3, and 5 (&AdjBaseLen, &AdjStrPos, and &AdjScnFLen, respectively) to be three-digit packed decimal values, SCNSTRCPP uses CHGVAR commands to convert the integer parameter values passed to the program (&BaseLen, &StrPos, and &ScanForLen) to packed decimal.
  2. he QCLSCAN API is called.
  3. The returned position value (&AdjPos) is examined by a Select group to send an escape message if an error occurred or return the position within the scanned string where the scanned-for pattern was found (with a value of zero indicating that the scanned-for string was not found).

Rather than sending escape messages when the API encountered an error, SCNSTRCPP could have returned the API-returned position value and left the checking for negative values (errors) to the user of the SCNSTR command. This would have allowed us to simply code the CHGVAR command found in the OTHERWISE command of the SELECT group and not have a SELECT group at all (greatly reducing the amount of executable code we wrote). Most users of CL commands, however, expect to monitor for errors by using the MONMSG command, not by examining command return values. So in keeping with normal command behavior, SCNSTRCPP uses escape messages to inform the users of error situations.

Testing the SCNSTR Command

In keeping with our last few articles, here is a test program for the SCNSTR command.

Pgm       Parm(&Base_In &ScanFor_In &StrPos_In)            

Dcl       Var(&Base_In)   Type(*Char) Len(32)            

Dcl       Var(&ScanFor_In) Type(*Char) Len(32)            

Dcl       Var(&StrPos_In) Type(*Dec)                      

                                                            

Dcl       Var(&Pos)       Type(*UInt)                    

Dcl       Var(&Pos_Char)   Type(*Char) Len(5)              

                                                            

ScnStr     ScanFor(&ScanFor_In) Base(&Base_In) +            

             Pos(&Pos) StrPos(&StrPos_In)                  

           MonMsg MsgID(ESC0003) Exec(Do)                  

             SndPgmMsg Msg('Invalid starting position') +  

                       ToPgmQ(*Ext)                        

             Return                                        

             EndDo                                          

                                                            

           MonMsg MsgID(ESC0004) Exec(Do)                

             SndPgmMsg Msg('SCANFOR longer than BASE') +  

                       ToPgmQ(*Ext)                      

             Return                                      

             EndDo                                        

                                                          

If         Cond(&Pos *NE 0) Then(Do)                      

             ChgVar Var(&Pos_Char) Value(&Pos)            

             SndPgmMsg Msg('String found at ' *Cat +      

                          &Pos_Char) +                  

                       ToPgmQ(*Ext)                      

             EndDo                                        

Else       Cmd(SndPgmMsg Msg('String not found') +        

                 ToPgmQ(*Ext))                            

                                                          

EndPgm                              

Assuming that the preceding source is stored in member USESCNSTR of source file QCLSRC, then you can use the following command to create the USESCNSTR program:

CrtBndCL Pgm(UseScnStr)

From the command line, we can now test a few scenarios.

Entering the command Call UseScnStr ('a cabbage' 'bag' 1) will result in the message 'String found at 00006'.

Entering the command Call UseScnStr ('a cabbage' 'bag' 3) will also result in the message 'String found at 00006' as the returned position is relative to the start of the first parameter and not the starting position specified by the third parameter.

Entering the command Call UseScnStr ('a cabbage' 'tag' 1) will result in the message 'String not found'.

Entering the command Call UseScnStr ('a cabbage' '' 1), where the second parameter is two contiguous apostrophes, will result in the message 'String found at 00002' as the command analyzer will treat two consecutive apostrophes as representing a single blank and a blank is found in the second position of the string 'a cabbage'.

Entering the command Call UseScnStr ('a cabbage' 'bag' 15) will result in the message 'Invalid starting position' as the first parameter is only nine bytes in length, so starting at position 15 is invalid.

Entering the command Call UseScnStr ('a cabbage' 'bag' 8) will result in the message 'SCANFOR longer than BASE' as the second parameter ('bag') is three bytes in length, but only two bytes of the first parameter, the eighth and ninth bytes, are to be scanned due to the starting position of eight and a total string length of nine for the first parameter.

Entering the command Call UseScnStr ('a cabbage' 'bag' 7) will result in the message 'String not found' as 'bag' is only found starting at position six, which is prior to the specified starting position of seven.

Entering the command Call UseScnStr ('a cabbage' 'age' 7) will result in the message 'String found at 00007'.

Next month, we'll look at some enhancements that can be made to our SCNSTR command, but for now SCNSTR rather closely mimics the behavior of the RPG built-in %scan.                    

More CL Questions?

Wondering how to accomplish a function in CL? Send your CL-related questions to me at This email address is being protected from spambots. You need JavaScript enabled to view it..

BLOG COMMENTS POWERED BY DISQUS

LATEST COMMENTS

Support MC Press Online

$

Book Reviews

Resource Center

  • SB Profound WC 5536 Have you been wondering about Node.js? Our free Node.js Webinar Series takes you from total beginner to creating a fully-functional IBM i Node.js business application. You can find Part 1 here. In Part 2 of our free Node.js Webinar Series, Brian May teaches you the different tooling options available for writing code, debugging, and using Git for version control. Brian will briefly discuss the different tools available, and demonstrate his preferred setup for Node development on IBM i or any platform. Attend this webinar to learn:

  • SB Profound WP 5539More than ever, there is a demand for IT to deliver innovation. Your IBM i has been an essential part of your business operations for years. However, your organization may struggle to maintain the current system and implement new projects. The thousands of customers we've worked with and surveyed state that expectations regarding the digital footprint and vision of the company are not aligned with the current IT environment.

  • SB HelpSystems ROBOT Generic IBM announced the E1080 servers using the latest Power10 processor in September 2021. The most powerful processor from IBM to date, Power10 is designed to handle the demands of doing business in today’s high-tech atmosphere, including running cloud applications, supporting big data, and managing AI workloads. But what does Power10 mean for your data center? In this recorded webinar, IBMers Dan Sundt and Dylan Boday join IBM Power Champion Tom Huntington for a discussion on why Power10 technology is the right strategic investment if you run IBM i, AIX, or Linux. In this action-packed hour, Tom will share trends from the IBM i and AIX user communities while Dan and Dylan dive into the tech specs for key hardware, including:

  • Magic MarkTRY the one package that solves all your document design and printing challenges on all your platforms. Produce bar code labels, electronic forms, ad hoc reports, and RFID tags – without programming! MarkMagic is the only document design and print solution that combines report writing, WYSIWYG label and forms design, and conditional printing in one integrated product. Make sure your data survives when catastrophe hits. Request your trial now!  Request Now.

  • SB HelpSystems ROBOT GenericForms of ransomware has been around for over 30 years, and with more and more organizations suffering attacks each year, it continues to endure. What has made ransomware such a durable threat and what is the best way to combat it? In order to prevent ransomware, organizations must first understand how it works.

  • SB HelpSystems ROBOT GenericIT security is a top priority for businesses around the world, but most IBM i pros don’t know where to begin—and most cybersecurity experts don’t know IBM i. In this session, Robin Tatam explores the business impact of lax IBM i security, the top vulnerabilities putting IBM i at risk, and the steps you can take to protect your organization. If you’re looking to avoid unexpected downtime or corrupted data, you don’t want to miss this session.

  • SB HelpSystems ROBOT GenericCan you trust all of your users all of the time? A typical end user receives 16 malicious emails each month, but only 17 percent of these phishing campaigns are reported to IT. Once an attack is underway, most organizations won’t discover the breach until six months later. A staggering amount of damage can occur in that time. Despite these risks, 93 percent of organizations are leaving their IBM i systems vulnerable to cybercrime. In this on-demand webinar, IBM i security experts Robin Tatam and Sandi Moore will reveal:

  • FORTRA Disaster protection is vital to every business. Yet, it often consists of patched together procedures that are prone to error. From automatic backups to data encryption to media management, Robot automates the routine (yet often complex) tasks of iSeries backup and recovery, saving you time and money and making the process safer and more reliable. Automate your backups with the Robot Backup and Recovery Solution. Key features include:

  • FORTRAManaging messages on your IBM i can be more than a full-time job if you have to do it manually. Messages need a response and resources must be monitored—often over multiple systems and across platforms. How can you be sure you won’t miss important system events? Automate your message center with the Robot Message Management Solution. Key features include:

  • FORTRAThe thought of printing, distributing, and storing iSeries reports manually may reduce you to tears. Paper and labor costs associated with report generation can spiral out of control. Mountains of paper threaten to swamp your files. Robot automates report bursting, distribution, bundling, and archiving, and offers secure, selective online report viewing. Manage your reports with the Robot Report Management Solution. Key features include:

  • FORTRAFor over 30 years, Robot has been a leader in systems management for IBM i. With batch job creation and scheduling at its core, the Robot Job Scheduling Solution reduces the opportunity for human error and helps you maintain service levels, automating even the biggest, most complex runbooks. Manage your job schedule with the Robot Job Scheduling Solution. Key features include:

  • LANSA Business users want new applications now. Market and regulatory pressures require faster application updates and delivery into production. Your IBM i developers may be approaching retirement, and you see no sure way to fill their positions with experienced developers. In addition, you may be caught between maintaining your existing applications and the uncertainty of moving to something new.

  • LANSAWhen it comes to creating your business applications, there are hundreds of coding platforms and programming languages to choose from. These options range from very complex traditional programming languages to Low-Code platforms where sometimes no traditional coding experience is needed. Download our whitepaper, The Power of Writing Code in a Low-Code Solution, and:

  • LANSASupply Chain is becoming increasingly complex and unpredictable. From raw materials for manufacturing to food supply chains, the journey from source to production to delivery to consumers is marred with inefficiencies, manual processes, shortages, recalls, counterfeits, and scandals. In this webinar, we discuss how:

  • The MC Resource Centers bring you the widest selection of white papers, trial software, and on-demand webcasts for you to choose from. >> Review the list of White Papers, Trial Software or On-Demand Webcast at the MC Press Resource Center. >> Add the items to yru Cart and complet he checkout process and submit

  • Profound Logic Have you been wondering about Node.js? Our free Node.js Webinar Series takes you from total beginner to creating a fully-functional IBM i Node.js business application.

  • SB Profound WC 5536Join us for this hour-long webcast that will explore:

  • Fortra IT managers hoping to find new IBM i talent are discovering that the pool of experienced RPG programmers and operators or administrators with intimate knowledge of the operating system and the applications that run on it is small. This begs the question: How will you manage the platform that supports such a big part of your business? This guide offers strategies and software suggestions to help you plan IT staffing and resources and smooth the transition after your AS/400 talent retires. Read on to learn: