View previous topic :: View next topic |
Author |
Message |
help_me Beginner
Joined: 03 Oct 2003 Posts: 11 Topics: 4
|
Posted: Sun Oct 05, 2003 8:49 pm Post subject: Finding Length of a Variable in COBOL... |
|
|
Hi,
Is there any way to Find out the Length of a Variable Declared as PIC X(200) But has only say "COBOL" and If Use the Length Function, It gives me as 200 only Not Trimming the Spaces.
I know there are certain Functions to achieve the same in Higher Versions of COBOL. But If I use the Function REVERSE, my COBOL COmpiler is Not recognizing the Functions and is failing in the Compiler stage giving Syntax Errors.
Is there any other way of Finding the Length without Parsing for the Variable Until for SPACES.
Can we use INSPECT Verb for the same. Please let me know
Thank you. |
|
Back to top |
|
 |
slade Intermediate
Joined: 07 Feb 2003 Posts: 266 Topics: 1 Location: Edison, NJ USA
|
Posted: Sun Oct 05, 2003 11:55 pm Post subject: |
|
|
Hi HM,
There are various ways to do what you want, but it's good to know the compiler version/environment you're working with and What you intend to do with the length of the char string within the variable.
Once you do that, you'll have your answer in no time (almost).
regards, Jack. |
|
Back to top |
|
 |
kolusu Site Admin

Joined: 26 Nov 2002 Posts: 12376 Topics: 75 Location: San Jose
|
Posted: Mon Oct 06, 2003 5:34 am Post subject: |
|
|
help_me,
The simplest way is to scan the string backwards using perform and find out the length of the string.
Code: |
WORKING-STORAGE SECTION.
01 W-STRING PIC X(60).
01 W-SUB PIC 9(02) VALUE 0.
01 S-ALPHABET-FOUND PIC X(01) VALUE 'N'.
PROCEDURE DIVISION.
MOVE 'I WANT TO FIND THE CORRECT LENGTH' TO W-STRING
PERFORM VARYING W-SUB FROM 60 BY -1 UNTIL W-SUB < 1
OR S-ALPHABET-FOUND = 'Y'
IF W-STRING (W-SUB : 1) NOT = SPACE
MOVE 'Y' TO S-ALPHABET-FOUND
DISPLAY 'THE LENGTH OF THE STRING:' W-SUB
END-IF
END-PERFORM
|
If your string does not embedded spaces in between then you can use STRING delimited by space and find out the length.
Hope this helps...
cheers
kolusu
PS: This may not be the optimal solution as I just got off bed.  |
|
Back to top |
|
 |
Oldie Beginner
Joined: 19 Dec 2002 Posts: 10 Topics: 3 Location: Munich, Germany
|
Posted: Tue Oct 21, 2003 7:46 am Post subject: |
|
|
You can use the
FUNCTION LENGTH (argument)
where argument may be a non-numeric literal or a data item of any class
or category except DBCS.
If the argument (or any data item subordinate to the argument) is
defined with the OCCURS clause DEPENDING ON phrase, then the length of
the specified argument is based on the value of the object of the
DEPENDING ON phrase at the time the LENGTH function is evaluated.
As an example of the LENGTH function:
FUNCTION LENGTH('ABC')
would return a value of 3.
Cheers
Oldie |
|
Back to top |
|
 |
|
|