This user exit can be used to dynamically test the format of the field.
My first question is: does it work?
Could someone tell me how this routine exactly works?
Kind regards,
Rudolf
This user exit can be used to dynamically test the format of the field.
My first question is: does it work?
Could someone tell me how this routine exactly works?
Kind regards,
Rudolf
I’ve never used USR1021. But I think it checks the content of the field.
Try this:
define data local
01 #usr1021
02 field (A80) init <'X/XABCDEFG'>
02 mask (A80) init <'X??A*FG'>
02 wildcard (A1) init <'?'>
02 asterisk (A1) init <'*'>
02 length (p2) init <20>
02 matched (A1)
*
end-define
callnat 'USR1021N' #usr1021
write '=' field (AL=79) '=' mask (AL=79) '=' length '=' matched
end
Nowadays everybody would do this job using the statement IF #field = MASK …
Thank you. Now it is clear. It only supports Wildcard and Asterisk. For a moment I was thinking that it also supported the other meta characters, like N and YYYY, but it doesn’t.
BTW, it is more useful than you think. Using MASK(…) can’t be used during runtime. The check is burned into the code during compile time. What if I want to change the mask when running the program. Do you see my point? That’s the problem with nowadays ;-).
Cheers,
Rudolf
define data local
01 #mymask (A10)
01 #field (A10) init <'20100114'>
end-define
*
#mymask := 'YYYYMMDD'
perform do-mask
#mymask := '10-19'
perform do-mask
*
define subroutine do-mask
if #field = MASK #mymask
write '#field matches' #mymask
else
write '#field does not match' #mymask
end-if
end-subroutine
end
Thnx Matthias. I didn’t know.
Maybe it’s a good idea to show an example with variable mask definition in the documentation (I’m really lazy). It only contains examples with constant masks.
Hi Matthias,
Just one more question. Suppose I would like to convert a alphanumeric string to I4 or F8. How can I easily test if the alphanumeric string can be converted using VAL (or do I need to create a dynamic mask from the input) ?
Cheers,
Rudolf
Use the IS function as shown below. Basically, IS means, “can this be converted to”, but it does not does the conversion, VAL does
DEFINE DATA LOCAL
1 #ALPHA (A5) INIT <‘12345’>
1 #I (I4)
1 #F (F4)
END-DEFINE
*
INCLUDE AATITLER
INCLUDE AASETC
IF #ALPHA IS (I4)
COMPUTE #I = VAL (#ALPHA)
WRITE 5T ‘=’ #ALPHA ‘=’ #I
ELSE
WRITE 5T ‘=’ #ALPHA ‘WILL NOT CONVERT TO I4’
END-IF
*
MOVE ‘QW123’ TO #ALPHA
IF #ALPHA IS (I4)
COMPUTE #I = VAL (#ALPHA)
WRITE 5T ‘=’ #ALPHA ‘=’ #I
ELSE
WRITE 5T ‘=’ #ALPHA ‘WILL NOT CONVERT TO I4’
END-IF
*
MOVE ‘12345’ TO #ALPHA
IF #ALPHA IS (F4)
COMPUTE #F = VAL (#ALPHA)
WRITE 5T ‘=’ #F ‘=’ #I
ELSE
WRITE 5T ‘=’ #F ‘WILL NOT CONVERT TO F4’
END-IF
END
PAGE # 1 DATE: 10-01-14
PROGRAM: ISVAL01 LIBRARY: INSIDE
#ALPHA: 12345 #I: 12345
#ALPHA: QW123 WILL NOT CONVERT TO I4
#F: +1.234500E+04 #I: 12345
steve
The difference between MASK and IS is described here:
That was very helpful. Thank you both.
BTW: First I thought, your initial question “to dynamically test the format of the field” means to determine the field type (i.e. Date, Alpha, Logical, …) at runtime.
That would be another interesting question. The answer is AFAIK: No chance.