Cobol TRIM



Support for OS/VS COBOL, VS COBOL II, COBOL for OS/390 & VM and Enterprise COBOL for z/OS

Cobol TRIM

Postby LasseH » Wed Dec 14, 2022 11:01 pm

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?
LasseH
 
Posts: 70
Joined: Mon Nov 08, 2010 2:51 pm
Has thanked: 7 times
Been thanked: 1 time

Re: Cobol TRIM

Postby Robert Sample » Wed Dec 14, 2022 11:14 pm

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.

These users thanked the author Robert Sample for the post:
LasseH (Thu Dec 15, 2022 12:48 am)
Robert Sample
Global moderator
 
Posts: 3720
Joined: Sat Dec 19, 2009 8:32 pm
Location: Dubuque, Iowa, USA
Has thanked: 1 time
Been thanked: 279 times

Re: Cobol TRIM

Postby sergeyken » Wed Dec 14, 2022 11:21 pm

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.
Javas and Pythons come and go, but JCL and SORT stay forever.
User avatar
sergeyken
 
Posts: 438
Joined: Wed Jul 24, 2019 10:12 pm
Has thanked: 7 times
Been thanked: 40 times

Re: Cobol TRIM

Postby LasseH » Thu Dec 15, 2022 12:41 am

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?
LasseH
 
Posts: 70
Joined: Mon Nov 08, 2010 2:51 pm
Has thanked: 7 times
Been thanked: 1 time

Re: Cobol TRIM

Postby LasseH » Thu Dec 15, 2022 12:47 am

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?
LasseH
 
Posts: 70
Joined: Mon Nov 08, 2010 2:51 pm
Has thanked: 7 times
Been thanked: 1 time

Re: Cobol TRIM

Postby Robert Sample » Thu Dec 15, 2022 1:48 am

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

These users thanked the author Robert Sample for the post:
LasseH (Thu Dec 15, 2022 3:08 pm)
Robert Sample
Global moderator
 
Posts: 3720
Joined: Sat Dec 19, 2009 8:32 pm
Location: Dubuque, Iowa, USA
Has thanked: 1 time
Been thanked: 279 times

Re: Cobol TRIM

Postby LasseH » Thu Dec 15, 2022 3:07 pm

tnxs, that's an ok solution
LasseH
 
Posts: 70
Joined: Mon Nov 08, 2010 2:51 pm
Has thanked: 7 times
Been thanked: 1 time

Re: Cobol TRIM

Postby LasseH » Thu Dec 15, 2022 3:23 pm

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.                  
 
LasseH
 
Posts: 70
Joined: Mon Nov 08, 2010 2:51 pm
Has thanked: 7 times
Been thanked: 1 time

Re: Cobol TRIM

Postby sergeyken » Thu Dec 15, 2022 6:08 pm

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.
Javas and Pythons come and go, but JCL and SORT stay forever.
User avatar
sergeyken
 
Posts: 438
Joined: Wed Jul 24, 2019 10:12 pm
Has thanked: 7 times
Been thanked: 40 times

Re: Cobol TRIM

Postby sergeyken » Thu Dec 15, 2022 6:36 pm

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?
Javas and Pythons come and go, but JCL and SORT stay forever.
User avatar
sergeyken
 
Posts: 438
Joined: Wed Jul 24, 2019 10:12 pm
Has thanked: 7 times
Been thanked: 40 times

Next

Return to IBM Cobol

 


  • Related topics
    Replies
    Views
    Last post