MVSFORUMS.com Forum Index MVSFORUMS.com
A Community of and for MVS Professionals
 
 FAQFAQ   SearchSearch   Quick Manuals   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Finding Length of a Variable in COBOL...

 
Post new topic   Reply to topic   printer-friendly view    MVSFORUMS.com Forum Index -> Application Programming
View previous topic :: View next topic  
Author Message
help_me
Beginner


Joined: 03 Oct 2003
Posts: 11
Topics: 4

PostPosted: Sun Oct 05, 2003 8:49 pm    Post subject: Finding Length of a Variable in COBOL... Reply with quote

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
View user's profile Send private message
slade
Intermediate


Joined: 07 Feb 2003
Posts: 266
Topics: 1
Location: Edison, NJ USA

PostPosted: Sun Oct 05, 2003 11:55 pm    Post subject: Reply with quote

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). Smile

regards, Jack.
Back to top
View user's profile Send private message
kolusu
Site Admin
Site Admin


Joined: 26 Nov 2002
Posts: 12376
Topics: 75
Location: San Jose

PostPosted: Mon Oct 06, 2003 5:34 am    Post subject: Reply with quote

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. Sad
Back to top
View user's profile Send private message Send e-mail Visit poster's website
Oldie
Beginner


Joined: 19 Dec 2002
Posts: 10
Topics: 3
Location: Munich, Germany

PostPosted: Tue Oct 21, 2003 7:46 am    Post subject: Reply with quote

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
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic   printer-friendly view    MVSFORUMS.com Forum Index -> Application Programming All times are GMT - 5 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


MVSFORUMS
Powered by phpBB © 2001, 2005 phpBB Group