IBM Mainframe Forum Index
 
Log In
 
IBM Mainframe Forum Index Mainframe: Search IBM Mainframe Forum: FAQ Register
 

Find the LENGTH of a String in COBOL


IBM Mainframe Forums -> COBOL Programming
Post new topic   Reply to topic
View previous topic :: View next topic  
Author Message
kranthikumarb

Active User


Joined: 02 Jan 2009
Posts: 115
Location: Hyderabad

PostPosted: Thu Nov 26, 2009 4:13 pm
Reply with quote

I know that this thread is quite old. But the way I looked for a solution after 3 years of the post, some one might want to know a solution after n years.

This is with out using any kind of functions.

Code:

 WORKING-STORAGE SECTION.                                       
   01 VAR1   PIC X(1000) VALUE SPACES.                           
   01 VAR2   PIC X(1000) VALUE SPACES.                           
   01 WS-LEN PIC 9(04) VALUE ZEROS.                             
   01 TOTAL PIC 9(04) VALUE ZEROS.                               
   01 I PIC 9(04).                                               
   01 J PIC 9(04).                                               
  PROCEDURE DIVISION.                                           
  100-MAIN-PARA.                                                 
        DISPLAY 'ENTER STRING :'                                 
        ACCEPT VAR1                                             
        MOVE 1 TO J                                             
* TO CALCULATE THE TOTAL LENGTH CAPACITY                                 
        INSPECT VAR1 TALLYING TOTAL FOR                         
        CHARACTERS BEFORE INITIAL '\0'                           
        DISPLAY 'TOTAL LENGTH CAPACITY OF THE VARIABLE IS ' TOTAL
        MOVE TOTAL TO I                                         
* STRING REVERSING                                               
         PERFORM UNTIL I = 0                                     
            MOVE VAR1(I:1) TO VAR2(J:1)                         
            COMPUTE J = J + 1                                   
            COMPUTE I = I - 1                                   
         END-PERFORM                                             
* END OF STRING REVERSING                                       
         INSPECT VAR2 TALLYING WS-LEN FOR LEADING SPACES         
         COMPUTE WS-LEN = TOTAL - WS-LEN                         
         DISPLAY 'LENGTH IS  :' WS-LEN                           
        STOP RUN.                                               


I am happy if this helps someone icon_smile.gif
Back to top
View user's profile Send private message
Robert Sample

Global Moderator


Joined: 06 Jun 2008
Posts: 8696
Location: Dubuque, Iowa, USA

PostPosted: Thu Nov 26, 2009 4:31 pm
Reply with quote

Quote:
CHARACTERS BEFORE INITIAL '\0'
You are aware, I hope, that COBOL does not support the C standard of using a hex '00' to denote the end of a string? What you are looking for here is two characters -- a backwards slash followed by a zero (hex F0) -- not a single byte of hex 00. What C defines as \0 is called in COBOL a LOW-VALUES byte. Furthermore, 3270 emulators do not terminate string data input with anything so the data accepted by the program won't have a terminator, unless the person entering the data keys one in.
Back to top
View user's profile Send private message
kranthikumarb

Active User


Joined: 02 Jan 2009
Posts: 115
Location: Hyderabad

PostPosted: Thu Nov 26, 2009 9:07 pm
Reply with quote

Hi,
But, In the above code the value in the variable is 1000.
How can we explain this?

I just want to know how are we getting a value of 1000 in total if it \0 doesnt say that it is the end of the allocated size for the variable.

Regards,
Kranthi Kumar
Back to top
View user's profile Send private message
dbzTHEdinosauer

Global Moderator


Joined: 20 Oct 2006
Posts: 6966
Location: porcelain throne

PostPosted: Thu Nov 26, 2009 10:40 pm
Reply with quote

Kranthi,

COBOL does not support the C standard of using a hex '00' to denote the end of a string

allocated size of variable is determined at compile time - not run time.

Admittedly, COBOL is not the best string manipulation language,
but if you would read the manuals, you won't have any problems.
Back to top
View user's profile Send private message
Robert Sample

Global Moderator


Joined: 06 Jun 2008
Posts: 8696
Location: Dubuque, Iowa, USA

PostPosted: Fri Nov 27, 2009 5:17 am
Reply with quote

If you define the variable as PIC X(1000), the variable length is 1000. Not 1, not 10, not 100, not depending upon where a \0 occurs -- the length is 1000 bytes -- always and permanently. Unlike C and similar languages, you cannot change the length of the variable at run time -- it will always and forever be the length set at compile time.
Back to top
View user's profile Send private message
dbzTHEdinosauer

Global Moderator


Joined: 20 Oct 2006
Posts: 6966
Location: porcelain throne

PostPosted: Fri Nov 27, 2009 6:18 am
Reply with quote

The original Enterprise Cobol Manual (several years ago),
had a comment that stipulated that, like C or JAVA,
the COBOL LENGTH function of a variable would return either
the compile time generated length,
or would reflect the length of a null (X'00') delineated string.

Subsequent versions of the manual have omitted the NULL delineated string concept

it is not in force, nor was it ever in force. The original manual was published in error
Back to top
View user's profile Send private message
dick scherrer

Moderator Emeritus


Joined: 23 Nov 2006
Posts: 19244
Location: Inside the Matrix

PostPosted: Fri Nov 27, 2009 7:46 am
Reply with quote

Hello,

Quote:
you cannot change the length of the variable at run time -- it will always and forever be the length set at compile time.
While length will remain constant; You can choose how much of the variable you use.

So you might consider calculating/determining the "length to use" and when "using" the field, specify reference modification and the calculated length-to-use for those statements. . .
Back to top
View user's profile Send private message
View previous topic :: :: View next topic  
Post new topic   Reply to topic View Bookmarks
All times are GMT + 6 Hours
Forum Index -> COBOL Programming Goto page 1, 2  Next

 


Similar Topics
Topic Forum Replies
No new posts Store the data for fixed length COBOL Programming 1
No new posts How to split large record length file... DFSORT/ICETOOL 10
No new posts Replace each space in cobol string wi... COBOL Programming 3
No new posts PARSE Syntax for not fix length word ... JCL & VSAM 7
No new posts COBOL -Linkage Section-Case Sensitive COBOL Programming 1
Search our Forums:

Back to Top