Page 1 of 2

Cobol TRIM

PostPosted: Wed Dec 14, 2022 11:01 pm
by LasseH
Need some guidance regarding TRIM

I do understand

01  wa-area  pic  x(9).
move trim('  HELLO  ',leading)  to  wa-area

when wa-area displayed it will be 'HELLO    '
 

But what happens with TRAILING or BOTH

01  wa-area  pic  x(9).
move trim('  HELLO  ',trailing)  to  wa-area

when wa-area displayed it will be '  HELLO    '
 

The only solution I can find out is to use "INSPECT REVERSE" to count chars for leading space to find out how many chars that is significant
Any other solutions?

Re: Cobol TRIM

PostPosted: Wed Dec 14, 2022 11:14 pm
by Robert Sample
Since you didn't tell us what your desired result is, it is difficult to know how to reply to you.

However, I will say that using TRIM for trailing and putting the result in a variable will usually not result in what you expect. Why? TRIM in this case would reduce the string to the number of characters (seven in your example) that end with the final non-blank character. It will then move the result into a nine-character variable and the move result means the last two characters of the variable will be set to blanks since the variable MUST have nine characters.

Re: Cobol TRIM

PostPosted: Wed Dec 14, 2022 11:21 pm
by sergeyken
You need to show here not what (probably) WILL BE displayed, but what ACTUALLY IS displayed

Your result is in contrary with the original description of the function TRIM.
Examples
- FUNCTION TRIM(" Hello, world! ", LEADING) returns "Hello, world! "
- FUNCTION TRIM(" Hello, world! ", TRAILING) returns " Hello, world!"
- FUNCTION TRIM(" Hello, world! ") returns "Hello, world!"
- FUNCTION TRIM(" ") returns ""
- FUNCTION TRIM("") returns ""

I recommend yo to carefully test your own code: the exact value of each variable at each step.

Re: Cobol TRIM

PostPosted: Thu Dec 15, 2022 12:41 am
by LasseH
sergeyken

Yes I read the manual also and the result shown there is if You do a
display FUNCTION TRIM(" Hello, world! ", TRAILING) returns " Hello, world!"

But what use do I have in a MOVE situation?

Re: Cobol TRIM

PostPosted: Thu Dec 15, 2022 12:47 am
by LasseH
Robert Sample wrote:Since you didn't tell us what your desired result is, it is difficult to know how to reply to you.

However, I will say that using TRIM for trailing and putting the result in a variable will usually not result in what you expect. Why? TRIM in this case would reduce the string to the number of characters (seven in your example) that end with the final non-blank character. It will then move the result into a nine-character variable and the move result means the last two characters of the variable will be set to blanks since the variable MUST have nine characters.


This type of functions can only return 1 variable as result.
I have to caluculate the actual length of significant characters by myself, correct?
My question was mostly, do we have a smart way of finding the length?

Re: Cobol TRIM

PostPosted: Thu Dec 15, 2022 1:48 am
by Robert Sample
do we have a smart way of finding the length?

WORKING-STORAGE SECTION.                                    
 01  WS-AREA                     PIC X(09).                  
 01  WS-LENGTH                   PIC 9(09) VALUE ZERO.      
 PROCEDURE DIVISION.                                        
     MOVE FUNCTION TRIM('  HELLO  ', TRAILING) TO WS-AREA.  
     DISPLAY '>' WS-AREA '<'.                                        
     COMPUTE  WS-LENGTH = FUNCTION LENGTH(                  
         FUNCTION TRIM('  HELLO  ', TRAILING) )              
         .                                                  
     DISPLAY 'LENGTH: ' WS-LENGTH.                          
produces results of
>  HELLO  <      
LENGTH: 000000007

Re: Cobol TRIM

PostPosted: Thu Dec 15, 2022 3:07 pm
by LasseH
tnxs, that's an ok solution

Re: Cobol TRIM

PostPosted: Thu Dec 15, 2022 3:23 pm
by LasseH
Trying to use a variable containing the option leading/trailing/blank in the "call" to TRIM but the compiler rejects that.
I understand the errormessage, but....
Is it possible to do that in some way?


01 option pix x(7).
move 'leading' to option
move function trim(checkfield, option) to field

Result
IGYPS2292-S Function argument "OPTION" did not have the correct type for function
            "TRIM".  The statement was discarded.                  
 

Re: Cobol TRIM

PostPosted: Thu Dec 15, 2022 6:08 pm
by sergeyken
LasseH wrote:Trying to use a variable containing the option leading/trailing/blank in the "call" to TRIM but the compiler rejects that.
I understand the error message, but....
Is it possible to do that in some way?

Result
IGYPS2292-S Function argument "OPTION" did not have the correct type for function
            "TRIM".  The statement was discarded.                  


This doesn't make any sense, from COBOL syntax point of view.

The COBOL options must be specified as they are described; no intermediate variable is possible.

Re: Cobol TRIM

PostPosted: Thu Dec 15, 2022 6:36 pm
by sergeyken
LasseH wrote:Trying to use a variable containing the option leading/trailing/blank in the "call" to TRIM but the compiler rejects that.
I understand the errormessage, but....
Is it possible to do that in some way?


01 option pix x(7).
move 'leading' to option
move function trim(checkfield, option) to field

Result
IGYPS2292-S Function argument "OPTION" did not have the correct type for function
            "TRIM".  The statement was discarded.                  
 

FYI:
There is no correct option PIX in COBOL.
Since you did not get a compiler error on that line, I suspect that your demonstration is either partial fake, or fake in full?