View previous topic :: View next topic
|
Author |
Message |
kranthikumarb
Active User
Joined: 02 Jan 2009 Posts: 115 Location: Hyderabad
|
|
|
|
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 |
|
Back to top |
|
|
Robert Sample
Global Moderator
Joined: 06 Jun 2008 Posts: 8696 Location: Dubuque, Iowa, USA
|
|
|
|
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 |
|
|
kranthikumarb
Active User
Joined: 02 Jan 2009 Posts: 115 Location: Hyderabad
|
|
|
|
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 |
|
|
dbzTHEdinosauer
Global Moderator
Joined: 20 Oct 2006 Posts: 6966 Location: porcelain throne
|
|
|
|
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 |
|
|
Robert Sample
Global Moderator
Joined: 06 Jun 2008 Posts: 8696 Location: Dubuque, Iowa, USA
|
|
|
|
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 |
|
|
dbzTHEdinosauer
Global Moderator
Joined: 20 Oct 2006 Posts: 6966 Location: porcelain throne
|
|
|
|
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 |
|
|
dick scherrer
Moderator Emeritus
Joined: 23 Nov 2006 Posts: 19244 Location: Inside the Matrix
|
|
|
|
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 |
|
|
|