View previous topic :: View next topic
|
Author |
Message |
vardhan0007
New User
Joined: 05 Jun 2006 Posts: 51 Location: Bangalore,India
|
|
|
|
Hi,
I have requirement to check few scenarios listed below.
01 WS-VAR PIC X(08)
I should validate the above variable,
i)It should not be ZEROS --->Taken care
e.g., 0000000
ii)should not be spaces----->Taken care
iii)Should not have any spaces between numbers of aplhabets.--->Need suggestions
e.g., 12 34567---->I should display error message
ab cdefg----->I should display error message
I am trying to validate each byte by byte using PERFORM. Is there any better way that I can do it?
Thanks
Vardhan |
|
Back to top |
|
|
Bill Woodger
Moderator Emeritus
Joined: 09 Mar 2011 Posts: 7309 Location: Inside the Matrix
|
|
|
|
Have a look at INSPECT TALLYING. |
|
Back to top |
|
|
Marso
REXX Moderator
Joined: 13 Mar 2006 Posts: 1353 Location: Israel
|
|
|
|
Can you have spaces at the beginning or at the end? |
|
Back to top |
|
|
Bill O'Boyle
CICS Moderator
Joined: 14 Jan 2008 Posts: 2501 Location: Atlanta, Georgia, USA
|
|
|
|
Use the TALLY special-register (initialize to ZERO first), with the INSPECT TALLYING as Bill has suggested.
However, once the INSPECT is complete and TALLY is non-zero, then what?
Do you need to reformat the data, such as left-justified with low-order SPACES or right-justified with high-order SPACES or just a simple notification via a DISPLAY will suffice?
Mr. Bill |
|
Back to top |
|
|
Chiranjeevi9
New User
Joined: 14 Dec 2011 Posts: 11 Location: India
|
|
|
|
As Bill Suggested INSPECT with TALLYING is enough for this scenario.
EX: INSPECT WS-VAR TALLYING COUNT-1 FOR ALL SPACES.
If COUNT-1 greaterthan ZERO means, found spaces in your cobol item(WS-VAR). So DISPLAY your error message when the COUNT-1 > ZERO.
|
|
Back to top |
|
|
Marso
REXX Moderator
Joined: 13 Mar 2006 Posts: 1353 Location: Israel
|
|
|
|
Marso wrote: |
Can you have spaces at the beginning or at the end? |
Depending on the complete requirement, suggestions may or may not work... |
|
Back to top |
|
|
Bill Woodger
Moderator Emeritus
Joined: 09 Mar 2011 Posts: 7309 Location: Inside the Matrix
|
|
|
|
So right Marso.
Code: |
234567
23 567
8
1
1 3
1234567
2345678
23 5678
|
Would all of the above be invalid? Same with the alphas.
Requires more thought about how to use INSPECT and probably FUNCTION REVERSE, for convenience.
We can even get on to whether a mix of numbers and alpahas are valid. What about "special characters"? What about non-display data?
If the full requirement is known, it could well be best to do a loop testing for each being valid rather than trying to pick the invalid. |
|
Back to top |
|
|
Bill O'Boyle
CICS Moderator
Joined: 14 Jan 2008 Posts: 2501 Location: Atlanta, Georgia, USA
|
|
Back to top |
|
|
vardhan0007
New User
Joined: 05 Jun 2006 Posts: 51 Location: Bangalore,India
|
|
|
|
Hi All,
Thank you for the replies. Below is my code.
05 WS-NEW-KYWD PIC X(08).
01 WS-NEW-KEYWORD
05 WS-NEW-KEYWORD OCCURS 8 INDEXED BY WS-IDX PIC X.
MOVE WS-NEW-KYWD TO WS-NEW-KEYWORD
INSPECT WS-NEW-KYWD TALLYING WS-CNT FOR ALL SPACES
MOVE 0 TO WS-FLG
MOVE 0 TO WS-ZERO-CNT
IF WS-CNT < 8
COMPUTE WS-CNT = 8 - WS-CNT
SET WS-IDX TO 1
PERFORM UNTIL WS-IDX > WS-CNT
IF WS-NEW-KEYWORD (WS-IDX) = ZERO
ADD 1 TO WS-ZERO-CNT
END-IF
IF WS-NEW-KEYWORD (WS-IDX) = SPACE
MOVE 1 TO WS-FLG
END-IF
SET WS-IDX UP BY 1
END-PERFORM
DISPLAY 'WS-FLG:' WS-FLG UPON CONSOLE
IF (WS-FLG = 1 OR ( WS-CNT = WS-ZERO-CNT ))
DISPLAY '** PLEASE ENTER VALID DERIVED KEYWORD **'
END-IF
END-IF |
|
Back to top |
|
|
Bill Woodger
Moderator Emeritus
Joined: 09 Mar 2011 Posts: 7309 Location: Inside the Matrix
|
|
|
|
If leading/trailing blanks are not valid, you have overcomplicated things by sticking to your byte-at-a-time approach.
Code: |
01 WS-COUNT-BLANKS-IN-NEW-KEYWRD COMP PIC S9(4).
88 WS-CBNK-THERE-ARE-SOME-BLANKS VALUE +1 THRU +7.
88 WS-CBNK-THERE-ARE-ONLY-BLANKS VALUE +8.
INSPECT WS-NEW-KYWD
TALLYING WS-COUNT-BLANKS-IN-NEW-KEYWRD
FOR SPACE
IF ( WS-CBNK-THERE-ARE-SOME-BLANKS )
OR ( WS-NEW-KEYWD EQUAL TO ZERO )
DISPLAY '** PLEASE ENTER VALID DERIVED KEYWORD **'
END-IF |
I have left out any reference to WS-CBNK-THERE-ARE-ONLY-BLANKS as your code ignores the all blank case.
You are using WS-CNT for two purposes at the same time. Firstly to count blanks, and then as an arbitrary value which only has a meaning for the rest of the code when it is 8.
You have a duplicate definition of WS-NEW-KEYWORD, I don't know how that works. I don't know what the "UPON CONSOLE" is about, are you running it in foreground?
What are you doing with the KEYWRD afterwards? If it is validated against a value in the program, all of the above can be avoided anyway. |
|
Back to top |
|
|
lindovaldolpn
New User
Joined: 25 Feb 2010 Posts: 16 Location: Brazil
|
|
|
|
It´s a another way
-------------------
01 WS-VAR PIC X(08)
77 BYTE1 PIC X VALUE SPACES.
77 BYTE2 PIC X VALUE SPACES.
UNSTRING WS-VAR
INTO BYTE1 DELIMITED BY SPACE
BYTE2
END-UNSTRING
IF BYTE2 NOT EQUAL SPACES
NOTE there are at least 1 byte with spaces
END-IF
Lindovaldo |
|
Back to top |
|
|
Bill Woodger
Moderator Emeritus
Joined: 09 Mar 2011 Posts: 7309 Location: Inside the Matrix
|
|
|
|
Have you tried this with one or more trailing spaces? What happens?
What happens when you code this, an embedded space is found, and you go on to test the next value? |
|
Back to top |
|
|
lindovaldolpn
New User
Joined: 25 Feb 2010 Posts: 16 Location: Brazil
|
|
|
|
it´s new source file.
ID DIVISION.
PROGRAM-ID. PGM1.
ENVIRONMENT DIVISION.
CONFIGURATION SECTION.
DATA DIVISION.
WORKING-STORAGE SECTION.
77 TALLY PIC S9(4) COMP.
77 WS-VAR PIC X(08).
77 BYTE1 PIC X(01) VALUE SPACES.
77 BYTE2 PIC X(01) VALUE SPACES.
PROCEDURE DIVISION.
INICIO.
DISPLAY 'ACCEPT NUMBER'.
ACCEPT WS-VAR.
IF WS-VAR EQUAL 'EXIT' STOP RUN.
MOVE ZEROS TO TALLY.
UNSTRING WS-VAR DELIMITED BY SPACE
INTO BYTE1
BYTE2 TALLYING IN TALLY.
DISPLAY 'TALLY=' TALLY, ',WS-VAR=(' WS-VAR ')'.
IF TALLY GREATER 1
DISPLAY 'THERE ARE AT LEAST 1 BYTE'
ELSE
DISPLAY 'OK'.
GO INICIO.
When I run...
(SPLXWAA8XA) C:\COBOL>PGM1
ACCEPT NUMBER
12345678
TALLY=+0001,WS-VAR=(12345678)
OK
ACCEPT NUMBER
1 3 5 7
TALLY=+0002,WS-VAR=(1 3 5 7 )
THERE ARE AT LEAST 1 BYTE
ACCEPT NUMBER
2 4 6 8
TALLY=+0002,WS-VAR=( 2 4 6 8)
THERE ARE AT LEAST 1 BYTE
ACCEPT NUMBER
1 8
TALLY=+0002,WS-VAR=(1 8)
THERE ARE AT LEAST 1 BYTE
ACCEPT NUMBER
1
TALLY=+0002,WS-VAR=(1 )
THERE ARE AT LEAST 1 BYTE
ACCEPT NUMBER
EXIT
(SPLXWAA8XA) C:\COBOL>
I hope that it will be solve your problem. |
|
Back to top |
|
|
Bill Woodger
Moderator Emeritus
Joined: 09 Mar 2011 Posts: 7309 Location: Inside the Matrix
|
|
|
|
Mmm...
So, you've changed your mind but want to keep it. You define two extra fields, populated (maybe) by UNSTRING, neither of which you use, just to provide an alternative to INSPECT TALLYING? Are you showing how it might be done on a PC, rather than a mainframe? Any-old-code-will-do, as long as the "right" answer pops out at the end?
And, of oourse, having been asked specifically about trailing space in your first "solution", you've tested this one with leading space, haven't you? |
|
Back to top |
|
|
lindovaldolpn
New User
Joined: 25 Feb 2010 Posts: 16 Location: Brazil
|
|
|
|
Ok, I agree with you.
I add an option using INSPECT, I think it is a better way.
MOVE ZEROS TO COUNT1
INSPECT WS-VAR TALLYING COUNT1 FOR ALL SPACE.
IF COUNT1 GREATER ZEROS
DISPLAY 'INSPECT - THERE ARE ' COUNT1 ' BYTE/S'
ELSE
DISPLAY 'INSPECT - OK'.
Mainframe with JES3
EDIT xxxxxxxx.STS.JOBS(JPGM1) - 01.01 Columns 00001 00072
Command ===> Scroll ===> CSR
****** ***************************** Top of Data ******************************
000001 //F334021T JOB '9999999,1,SPEC',F334097,CLASS=D,MSGCLASS=S
000002 //*MAIN SYSTEM=A4
000003 //*
000004 //STEP1 EXEC PGM=PGM1
000005 //STEPLIB DD DSN=F334021.STS.LINKLIB2,DISP=SHR
000006 //SYSPRINT DD SYSOUT=*
000007 //SYSOUT DD SYSOUT=*
000008 //SYSIN DD *
000009 12345678
000010 1 3 5 7
000011 2 4 6 8
000012 1 8
000013 1
000014 EXIT
****** **************************** Bottom of Data ****************************
. . . . . . . . . . . . . . . . . . . . . . . . . . .
Jobs Resources Devices Tools Filter View Options Help
-------------------------------------------------------------------------------
F334021T JOB22679 < .STEP1 .SYSOUU > Line 1 of 21
Command ===> Scroll ===> CSR
Current Find Text: Dataset 1 of 1
---+----1----+----2----+----3----+----4----+----5----+----6----+----7----+---->
ACCEPT NUMBER
TALLY=00001,WS-VAR=(12345678)
OK
INSPECT - OK
ACCEPT NUMBER
TALLY=00002,WS-VAR=(1 3 5 7 )
THERE ARE AT LEAST 1 BYTE
INSPECT - THERE ARE 0004 BYTE/S
ACCEPT NUMBER
TALLY=00002,WS-VAR=( 2 4 6 8)
THERE ARE AT LEAST 1 BYTE
INSPECT - THERE ARE 0004 BYTE/S
ACCEPT NUMBER
TALLY=00002,WS-VAR=(1 8)
THERE ARE AT LEAST 1 BYTE
INSPECT - THERE ARE 0006 BYTE/S
ACCEPT NUMBER
TALLY=00002,WS-VAR=(1 )
THERE ARE AT LEAST 1 BYTE
INSPECT - THERE ARE 0007 BYTE/S
ACCEPT NUMBER
Lindovaldo |
|
Back to top |
|
|
vardhan0007
New User
Joined: 05 Jun 2006 Posts: 51 Location: Bangalore,India
|
|
|
|
Hi Bill,
01 WS-COUNT-BLANKS-IN-NEW-KEYWRD COMP PIC S9(4).
88 WS-CBNK-THERE-ARE-SOME-BLANKS VALUE +1 THRU +7.
88 WS-CBNK-THERE-ARE-ONLY-BLANKS VALUE +8.
INSPECT WS-NEW-KYWD
TALLYING WS-COUNT-BLANKS-IN-NEW-KEYWRD
FOR SPACE
IF ( WS-CBNK-THERE-ARE-SOME-BLANKS )
OR ( WS-NEW-KEYWD EQUAL TO ZERO )
DISPLAY '** PLEASE ENTER VALID DERIVED KEYWORD **'
END-IF
The above code will even restrict me to enter any space in the field. i.e.,
12345 <---last 3 bytes spaces
12 <---Last 6 bytes spaces
1 <---Last 7 bytes spaces.
Yes I am already handling all spaces and zeros in another paragraph.
This is an IMS DC program hence I am using UPON CONSOLE statement for my debugging purpose.
Thanks again.
-Vardhan |
|
Back to top |
|
|
|