View previous topic :: View next topic |
Author |
Message |
enge Beginner
Joined: 12 Oct 2004 Posts: 78 Topics: 39
|
Posted: Thu Nov 06, 2008 5:37 am Post subject: shrink a string |
|
|
hello everyone,
i need to transfrom a string pic x(12) in in the same string.
how can i do?
thanks |
|
Back to top |
|
|
dbzTHEdinosauer Supermod
Joined: 20 Oct 2006 Posts: 1411 Topics: 26 Location: germany
|
Posted: Thu Nov 06, 2008 10:16 am Post subject: |
|
|
First of all, in COBOL, the only way to 'shrink' a string is to move it to a field that is smaller.
What you are refering to, I would call: Left justification, removing spaces, which by the way - DFSORT does a good job of doing.
This UNTESTED CODE will work
if you only have two words in your string.
If you can have more than 2 words,
then you need to add a WS-WORK-n for each additional word.
Code: |
05 YOUR-ORIGINAL-FIELD PIC X(12).
05 WS-WORK-1 PIC X(12).
05 WS-WORK-2 PIC X(12).
UNSTRING YOUR-ORIGINAL-FIELD
DELIMITED BY ALL SPACES
INTO WS-WORK-1
WS-WORK-2
END-UNSTRING
MOVE SPACES TO YOUR-ORIGINAL-FIELD
STRING WS-WORK-1
DELIMITED BY ALL SPACES
WS-WORK-2
DELIMITED BY ALL SPACES
INTO YOUR-ORIGINAL-FIELD
END-STRING
|
_________________ Dick Brenholtz
American living in Varel, Germany
Last edited by dbzTHEdinosauer on Thu Nov 06, 2008 11:22 am; edited 2 times in total |
|
Back to top |
|
|
jsharon1248 Intermediate
Joined: 08 Aug 2007 Posts: 291 Topics: 2 Location: Chicago
|
Posted: Thu Nov 06, 2008 10:17 am Post subject: |
|
|
For this task, I'd recommend just coding a PERFORM UNTIL that increments an index (or subscript if you prefer). You'll need a second pointer to store the current next available position in the receiving field. MOVE the characters that you want to keep 1 by 1; only bump the next available position pointer when you actually MOVE a character. Here's some untested code that should get you moving in the right direction:
Code: | INITIALIZE RECEIVING-FLD
MOVE +1 TO PTR2
MOVE LENGTH OF RECEIVING-FLD TO RECEIVING-FLD-L
MOVE LENGTH OF SENDING-FLD TO SENDING-FLD-L
PERFORM VARYING PTR1 FROM +1 BY +1
UNTIL PTR1 > SENDING-FLD-L
IF PTR2 <= RECEIVING-FLD-L
IF SENDING-FLD(PTR1:1) NOT = SPACE
MOVE SENDING-FLD(PTR1:1) TO RECEIVING-FLD(PTR2:1)
END-IF
END-IF
END-PERFORM |
|
|
Back to top |
|
|
enge Beginner
Joined: 12 Oct 2004 Posts: 78 Topics: 39
|
Posted: Thu Nov 06, 2008 11:29 am Post subject: |
|
|
good solutions.
i choose the last one.
thank a lot. |
|
Back to top |
|
|
dbzTHEdinosauer Supermod
Joined: 20 Oct 2006 Posts: 1411 Topics: 26 Location: germany
|
Posted: Thu Nov 06, 2008 12:18 pm Post subject: |
|
|
enge,
jsharon1248's solution will work if you add a line to bump PTR2 if it is used (second if is true). _________________ Dick Brenholtz
American living in Varel, Germany |
|
Back to top |
|
|
jsharon1248 Intermediate
Joined: 08 Aug 2007 Posts: 291 Topics: 2 Location: Chicago
|
Posted: Thu Nov 06, 2008 2:27 pm Post subject: |
|
|
DOH!!
Should've paid more attention to what I wrote. Thanks for the second set of eyes dbz.
This is a little closer to what you'll need.
Code: | INITIALIZE RECEIVING-FLD
MOVE +1 TO PTR2
MOVE LENGTH OF RECEIVING-FLD TO RECEIVING-FLD-L
MOVE LENGTH OF SENDING-FLD TO SENDING-FLD-L
PERFORM VARYING PTR1 FROM +1 BY +1
UNTIL PTR1 > SENDING-FLD-L
IF PTR2 <= RECEIVING-FLD-L
IF SENDING-FLD(PTR1:1) NOT = SPACE
MOVE SENDING-FLD(PTR1:1) TO RECEIVING-FLD(PTR2:1)
ADD +1 to PTR2
END-IF
END-IF
END-PERFORM |
|
|
Back to top |
|
|
dbzTHEdinosauer Supermod
Joined: 20 Oct 2006 Posts: 1411 Topics: 26 Location: germany
|
Posted: Fri Nov 07, 2008 3:16 am Post subject: |
|
|
no problem jsharon1248. I just had to spend about an hour apologizing and amending several posts that I made this week.
i should have sent a private message to you, so that you could have amended it. It was not necessary to blow my own horn. _________________ Dick Brenholtz
American living in Varel, Germany |
|
Back to top |
|
|
enge Beginner
Joined: 12 Oct 2004 Posts: 78 Topics: 39
|
Posted: Fri Nov 07, 2008 8:32 am Post subject: |
|
|
great work ! thanks you again. |
|
Back to top |
|
|
|
|