Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to check in-between spaces?

Status
Not open for further replies.

jiruld

Programmer
Dec 12, 2007
1
A2
Hi,

Can you help me come up with a code or routine that will check if the string has in-between spaces or space(s) in front?

Example:
Input PIC X(12)

The following case is considered INVALID:
1. ' Testing '
2. ' Testing '
2. 'T esting '
3. 'Te sting '
4. 'Testin g'

Thanks,
Jerold :)
 
Try:
Code:
UNSTRING INPUT-VAR 
     DELIMITED BY ALL SPACES
          INTO OUTPUT-TABLE (1) OUTPUT-TABLE (2)
      TALLYING PART-COUNT
END-UNSTRING.

And then you should be able to tell by the results it produces.
 
Jerold,

if the "4" (5) samples you gave are truly all examples of invalid data, then I would suggest something like the following:
Code:
MOVE ZERO TO SPACE-COUNT
INSPECT INPUT-FIELD (1:INPUT-LENGTH)
        TALLYING SPACE-COUNT FOR ALL SPACES
IF  SPACE-COUNT > ZERO
    DISPLAY "Input is Invalid!"
ELSE
    DISPLAY "Input is Valid"
END-IF

The hard part is knowing truly how long your input data is!

Code what you mean,
and mean what you code!
But by all means post your code!

Razalas
 
Ok, this is ridiculous.
Code:
77 x1 binary.
77 x2 binary.
. . .
if input-var not = space
    perform varying x1 from length of input-var by -1 until input-var(x1:1) not = space
        continue
    end-perform
    move zero to x2
    inspect input-var(1:1) tallying x2 for all spaces
    if x2 > zero
        result is bad
    end-if
end-if
Some people suggest using the REVERSE function and INSPECT to find the length of the text, but unless it has hardware support, the REVERSE function is expensive. Many years ago I was reviewing the new hardware functions for our new IBM mainframe. I found a REVERSE function and wondered what it was for. In my 45 year's of programming, I have needed to reverse a field once!
 
I don't think anyone is suggesting to reverse the text. Besides for mine (which was tested), I don't think knowing the input length is too necessary. Of course, efficiency is another matter for all these varied solutions, but that's a whole other topic.
 
input pic x(12)
text1 pic x(12) value spaces
text2 pic x(12) value spaces

unstring input delimited by " " into text1, text2.
if input = spaces or text2 not = spaces display "input is invalid
 
mrregan:
Your solution is similar to Glenn9999 except he delimited by All SPACE which is what is needed. If the input has 2 leading spaces, with your delimiter, each space is a delimiter and both receiving fields end up with spaces.
ALL will treat both spaces as one delimiter.

A combination of Glenn's UNSTRING and your IF looks like the simplest solution.
 
If I have to parse the question the need is to find:
"in-between spaces or space(s) in front?"

Then sample #1 is a valid assumption.
 
No one has addressed the question of whether a completly bank input field is valid.
 
Since a question is coming up of a best solution, etc, etc here's what I coded when I originally posted to this thread:

Code:
 IDENTIFICATION DIVISION.
 PROGRAM-ID. TEST1.
 ENVIRONMENT DIVISION.
 DATA DIVISION.
 WORKING-STORAGE SECTION.
 01  PROC-VARS.
     05  INPUT-VAR                  PIC X(12).
     05  OUTPUT-TABLE               PIC X(12) OCCURS 2.
     05  PART-COUNT                 PIC S9(4) BINARY.
 PROCEDURE DIVISION.
     PERFORM UNTIL INPUT-VAR = "QUIT"
       DISPLAY "ENTER VALUE: "
       ACCEPT INPUT-VAR FROM CONSOLE
       MOVE 0 TO PART-COUNT
       MOVE SPACES TO OUTPUT-TABLE (1) OUTPUT-TABLE (2)
       UNSTRING INPUT-VAR 
         DELIMITED BY ALL SPACES
            INTO OUTPUT-TABLE (1) OUTPUT-TABLE (2)
         TALLYING PART-COUNT
       END-UNSTRING
       IF (PART-COUNT > 1) OR (OUTPUT-TABLE (1) = SPACES) 
         DISPLAY "HAS SPACES"
       ELSE
         DISPLAY "DOES NOT HAVE SPACES"
       END-IF
       DISPLAY "PART COUNT: " PART-COUNT
       DISPLAY "1: " OUTPUT-TABLE (1)
       DISPLAY "2: " OUTPUT-TABLE (2)
     END-PERFORM.
     GOBACK.

No one has addressed the question of whether a completly bank input field is valid.

If I recall in my testing, all spaces was considered invalid. But the OP hasn't stated that, indeed.
 
Well, Glenn, your solution is the simplest to code, and considering the speed of today's computers, probably the best.

I think the OP has left the room.

I learned computers when the speed of an instruction was measured in many milliseconds. One problem involving calulations of 200 digits. Multiply was faster than Divide, so we precalculated the inverse of the divisor and used a Multiply. Only problem, the time for Multiply was a small factor times the square of the number of digits. The time for Divide was a large factor times the (number of digits + the sum of the digits in the quotient) Multiply timeing: several hours per instruction. Divide timing: about a second per instruction. We reconverted the problem back to using a Divide.
 
(one less instruction than above)

input pic x(12)
text1 pic x(12) value spaces

unstring input delimited by " " into text1.
if input = spaces or text1 not = input
display "input is invalid" .



 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top