Exemple – Quantité de Caractères

Exemple 1

IDENTIFICATION DIVISION.
PROGRAM-ID. CARACTERES.
DATA DIVISION. 
WORKING-STORAGE SECTION.
01  WS-AUXILIARES.
    05  WS-FIN       PIC 9(09)  VALUE 0.
    05  WS-IND       PIC 9(09)  VALUE 1.
    05  WS-TAILLE    PIC 9(09)  VALUE 0.
    05  WS-RESULT    PIC 9(09)  VALUE 0.
    05  WS-CHAMP     PIC X(30) VALUE 'LA COM M   DU CO B OL  '.
    
PROCEDURE DIVISION.

COMPUTE WS-RESULT = FUNCTION LENGTH (WS-CHAMP)

MOVE 0 TO WS-FIN WS-TAILLE
PERFORM VARYING WS-IND FROM 1 BY 1 
     UNTIL WS-IND GREATER THAN WS-RESULT
     IF WS-CHAMP(WS-IND:1) NOT EQUAL SPACES
        ADD 1 TO WS-TAILLE
     END-IF
END-PERFORM

DISPLAY 'WS-TAILLE - ' WS-TAILLE.
STOP RUN.

Resultat du test:

WS-TAILLE - 000000013

Exemple 2

IDENTIFICATION DIVISION.
PROGRAM-ID. CARACTERES.
DATA DIVISION. 
WORKING-STORAGE SECTION.
01  WS-AUXILIARES.
    05  WS-FIN       PIC 9(09)  VALUE 0.
    05  WS-IND       PIC 9(09)  VALUE 1.
    05  WS-TAILLE    PIC 9(09)  VALUE 0.
    05  WS-RESULT    PIC 9(09)  VALUE 0.
    05  WS-CHAMP     PIC X(30) VALUE 'LA COM M   DU CO B     '.
    
PROCEDURE DIVISION.

COMPUTE WS-RESULT = FUNCTION LENGTH (WS-CHAMP)

MOVE 0 TO WS-FIN WS-TAILLE
PERFORM VARYING WS-IND FROM 1 BY 1 
     UNTIL WS-IND GREATER THAN WS-RESULT
     IF WS-CHAMP(WS-IND:1) NOT EQUAL SPACES
        ADD 1 TO WS-TAILLE
     END-IF
END-PERFORM

DISPLAY 'WS-TAILLE - ' WS-TAILLE.
STOP RUN.

Resultat du test:

WS-TAILLE - 000000011

Exemple 3

IDENTIFICATION DIVISION.
PROGRAM-ID. CARACTERES.
DATA DIVISION. 
WORKING-STORAGE SECTION.
01  WS-AUXILIARES.
    05  WS-FIN       PIC 9(09)  VALUE 0.
    05  WS-IND       PIC 9(09)  VALUE 1.
    05  WS-TAILLE   PIC 9(09)  VALUE 0.
    05  WS-RESULT    PIC 9(09)  VALUE 0.
    05  WS-CHAMP     PIC X(30) VALUE 'COMMUN AUT E1  1  1  1  11'.
    
PROCEDURE DIVISION.

COMPUTE WS-RESULT = FUNCTION LENGTH (WS-CHAMP)

MOVE 0 TO WS-FIM WS-TAILLE
PERFORM VARYING WS-IND FROM 1 BY 1 
     UNTIL WS-IND GREATER THAN WS-RESULT
     IF WS-CHAMP(WS-IND:1) NOT EQUAL SPACES
        ADD 1 TO WS-TAILLE
     END-IF
END-PERFORM

DISPLAY 'WS-TAILLE - ' WS-TAILLE.
STOP RUN.

Resultat du test:

WS-TAILLE - 000000016