%SCAN Built-In Functions in rpgle

%SCAN function is used to find the first position of the search argument in the source string. If a match is found then position of that matched position is returned else 0 is returned.

Format of %SCAN function is %SCAN (search argument : source string {: start})

1st parameter of the function is search element are are looking up in the source string.

2nd parameter is the source string in which we are doing the search.

3rd paramter denotes the position from where we want the start the search in source string.

The second parameter must be the same type as the first parameter. These paramter can be character, graphic, or UCS-2.

Seach arguments or source String can contain blanks in form of string or string padded with blank. While doing search those blank spaces are also taken into consideration.

Example –%scan built-in function in rpgle

 Columns . . . :    6  80                                              AMIT/QRPGSRC
 SEU==>                                                                     TESTRPG
 FMT *   *. 1 ...+... 2 ...+... 3 ...+... 4 ...+... 5 ...+... 6 ...+... 7 ...+... 8
        *************** Beginning of data *****************************************
0002.00 D sourceString    S             30A   inz ('Amit Jaiswal')
0003.00 D substring       S             30A   VARYING
0004.00 D pos             S              5U 0
0005.00 D len             S              5U 0
0007.00  /FREE
0009.00    len = %scan ('     ' : sourceString);
0010.00    DSPLY  len;
0012.00    pos = %scan (' ' : sourceString);
0013.00    substring=%subst (sourceString:1:pos);
0014.00    DSPLY  substring;
0016.00    substring=%subst (sourceString:pos+1:len);
0017.00    DSPLY  substring;
0019.00  /END-FREE
0020.00 C                   Seton                                        LR


DSPLY     13   
DSPLY  Amit    
DSPLY  Jaiswal  

Explanation of Output:

Here Source string has 30 char length. Hence in actual the Source string is 'Amit JaiswalXXXXXXXXXXXXXXXXXX' where X= 1 Blank space

Now lets analyse each Output.

(i) %scan (' ' : sourceString)

Here 5 blanks string searched in the source string. The first occurence of 5 blanks in the source string is 13th position. Hence Output=13.

(ii) %scan (' ' : sourceString)

Here 1 blank is searched in the source string. The first occurence of 1 blank in the source string is 5th position. Hence Output=%subst (sourceString:1:5) i.e. 'Amit'

(iii) %subst (sourceString:pos+1:len)

Here the output will be %subst (sourceString:6:13) i.e. 'Jaiswal'

User Comments:

Copyright © www.go4as400.com, 2013-2023. Copyright notice   Terms of services   Privacy policy