View previous topic :: View next topic
|
Author |
Message |
Madhanlal Currently Banned New User
Joined: 21 Apr 2007 Posts: 23 Location: Chennai
|
|
|
|
Hi all
I have a requirement like this:
I have to validate a field ws-inp pic x(9) in the following way:
'abc def'- invalid
' abcedf' - valid
'abcdef ' - valid
In short i ve to see there is no spaces in mid.
Can you tell me how to do this cobol? |
|
Back to top |
|
|
enrico-sorichetti
Superior Member
Joined: 14 Mar 2007 Posts: 10873 Location: italy
|
|
|
|
Loop thru the string using reference modification
no flags, just three loops in sequence
1st loop to skip leading blanks
2nd loop to skip non blanks
3rd loop to check that all the remaining chars are blanks
every loop starting index is the previous loop index at exit + 1
the housekeeping for the three loops has performance wise less impact
than using a single loop checking the flags needed to determine the string status ... ( two needed ) |
|
Back to top |
|
|
mmwife
Super Moderator
Joined: 30 May 2003 Posts: 1592
|
|
|
|
WITHDRAWN BY SENDER. |
|
Back to top |
|
|
enrico-sorichetti
Superior Member
Joined: 14 Mar 2007 Posts: 10873 Location: italy
|
|
|
|
Quote: |
WITHDRAWN BY SENDER. |
now You are making everybody curious |
|
Back to top |
|
|
mmwife
Super Moderator
Joined: 30 May 2003 Posts: 1592
|
|
|
|
Here's another solution. It's tested and looks OK, but test around the edges to be sure.
I wouldn't say this is better than Enrico's solution, but I was curious about some of the restrictions in the use of CLASS and INSPECT (if any) and thought I'd give it a go.
For the curious; I thought i saw an error in the code, so I withdrew the earlier post.
Code: |
.
.
.
.
ENVIRONMENT DIVISION.
CONFIGURATION SECTION.
SPECIAL-NAMES.
CLASS HAS-NO-SPACES X'00' THRU X'3F' X'41' THRU X'FF'.
.
.
.
.
****************************************************************
WORKING-STORAGE SECTION.
****************************************************************
01 WS-WORK-AREAS.
03 TST-FLD PIC X(08).
03 POS PIC S9(02) VALUE 0 COMP.
03 LEN PIC S9(02) VALUE 0 COMP.
.
.
.
.
****************************************************************
PROCEDURE DIVISION.
****************************************************************
MAIN-LINE.
MOVE ' 234' TO TST-FLD
IF TST-FLD = SPACES
PERFORM INVALID-RTN
ELSE
PERFORM CALC-START-N-LEN
PERFORM TEST-THE-FLD
END-IF
STOP RUN
.
CALC-START-N-LEN.
MOVE ZEROS TO POS
MOVE ZEROS TO LEN
INSPECT TST-FLD TALLYING POS FOR LEADING SPACES
ADD POS TO LEN
INSPECT FUNCTION REVERSE (TST-FLD)
TALLYING LEN FOR LEADING SPACES
COMPUTE LEN = LENGTH OF TST-FLD - LEN
ADD +1 TO POS
.
TEST-THE-FLD.
DISPLAY 'TST-FLD >' TST-FLD(POS:LEN) '< '
IF TST-FLD(POS:LEN) HAS-NO-SPACES
PERFORM INVALID-RTN
ELSE
PERFORM VALID-RTN
END-IF
.
VALID-RTN.
DISPLAY 'FLD IS VALID'
.
INVALID-RTN.
DISPLAY 'FLD IS INVALID'
. |
|
|
Back to top |
|
|
enrico-sorichetti
Superior Member
Joined: 14 Mar 2007 Posts: 10873 Location: italy
|
|
|
|
Quote: |
I wouldn't say this is better than Enrico's solution |
Hi Jack!
imagine how I could be worried about it
I do not speak cobolese, and I was just hinting a quick and dirty logic, not the real coding
/pure speculation on
in rexx I would have done somwhere along ..
Code: |
if strip(string) = word(strip(string),1) then ... ok
else ... error
|
in plain words
if the whole string is equal to the first blank delimited string then there are no interspersed blanks
/pure speculation of |
|
Back to top |
|
|
rehan
New User
Joined: 13 May 2007 Posts: 6 Location: India
|
|
|
|
U can use inspect statement to find spaces if u wanna throw error use inspect tally |
|
Back to top |
|
|
dick scherrer
Moderator Emeritus
Joined: 23 Nov 2006 Posts: 19244 Location: Inside the Matrix
|
|
|
|
Hello,
How does a simple inspect/tally determine if there are any embedded spaces (which are invalid) rather than leading or trailing spaces (which are valid)?
This is not a kiddie chat room, so please do not use kiddie chat slang |
|
Back to top |
|
|
Bill O'Boyle
CICS Moderator
Joined: 14 Jan 2008 Posts: 2501 Location: Atlanta, Georgia, USA
|
|
Back to top |
|
|
|