the position of the last character inside a string



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

the position of the last character inside a string

Postby vegafacundodaniel » Fri Aug 06, 2010 11:13 pm

Hello,
I'd want to know, for a string of 10 characters, what is the position of the last character wrote inside the string (or the first position after the last character wrote inside the string)

Example:

TOTO --------- String X(10).

MOVE 'ABCDEF ' to TOTO.

TOTO = 'ABCDEF '

the position will be 6. (or 7)

Thanks in advance
vegafacundodaniel
 
Posts: 61
Joined: Tue Jul 20, 2010 4:27 pm
Has thanked: 1 time
Been thanked: 0 time

Re: the position of the last character inside a string

Postby NicC » Fri Aug 06, 2010 11:25 pm

you posted the same query a few days ago and got the answer. Suggest you re-read it - it is only about 10 topics down the screen.
The problem I have is that people can explain things quickly but I can only comprehend slowly.
Regards
Nic
NicC
Global moderator
 
Posts: 3025
Joined: Sun Jul 04, 2010 12:13 am
Location: Pushing up the daisies (almost)
Has thanked: 4 times
Been thanked: 136 times

Re: the position of the last character inside a string

Postby Robert Sample » Fri Aug 06, 2010 11:27 pm

First, be aware that COBOL -- unlike C or similar languages -- does not have the concept of "string". A COBOL variable defined in WORKING-STORAGE as PIC X(10) will always contain 10 bytes -- never more, never less.

Second, you did not mention which COBOL compiler you are using -- VS COBOL II, for example, does not have some of the functionality of Enterprise COBOL for MVS & VM and the solution varies slightly by compiler.

Assuming you're using a reasonably recent COBOL compiler (always a dangerous assumption), you can either (1) use reference modification from byte 10 to 1 looking for the first non-blank character, or (2) use FUNCTION REVERSE on the variable and INSPECT tallying leading spaces, then subtract the tally count from 10.

A method that would work for all versions of COBOL would be to redefine the variable as a 10-byte array and look at each byte from 10 to 1 to find the first non-blank character.
Robert Sample
Global moderator
 
Posts: 3719
Joined: Sat Dec 19, 2009 8:32 pm
Location: Dubuque, Iowa, USA
Has thanked: 1 time
Been thanked: 279 times

Re: the position of the last character inside a string

Postby vegafacundodaniel » Wed Aug 11, 2010 12:24 am

You are right Nic. I posted the same query but I didn't want that.

After I gave my solution using only the INSPECT function, I got en error because my variable X(20) may have spaces between de characters.

For example :
TOTO = 'ABCDE HIJ LMN '

I didn't explain that before.... Sorry

Now, Instead of using the INSPECT function, I am using INSPECT FUNCTION REVERSE. It works fine !!!

Regards !
vegafacundodaniel
 
Posts: 61
Joined: Tue Jul 20, 2010 4:27 pm
Has thanked: 1 time
Been thanked: 0 time

Re: the position of the last character inside a string

Postby NicC » Wed Aug 11, 2010 1:38 am

Well, you shaould have just continued the same thread. Also, if you had fully read my post I said 'reversed string'. Anyway, you got there in the end!
The problem I have is that people can explain things quickly but I can only comprehend slowly.
Regards
Nic
NicC
Global moderator
 
Posts: 3025
Joined: Sun Jul 04, 2010 12:13 am
Location: Pushing up the daisies (almost)
Has thanked: 4 times
Been thanked: 136 times


Return to IBM Cobol

 


  • Related topics
    Replies
    Views
    Last post