View previous topic :: View next topic
|
Author |
Message |
kudamala
New User
Joined: 12 Sep 2008 Posts: 51 Location: Bangalore
|
|
|
|
Hi
I have a requirement to sort the data in a string variable in COBOL.
For eg I have a variable FIELD1 with a length of 11. where in this field the expected values are as follows:
ABC-FFG-CDX
FFG-ABC-CDX
DEF-PSQ-TPD etc.
the data contains 3 letters code seperated by a hyphen. For example if you see
ABC-FFG-CDX
FFG-ABC-CDX
both have the same codes, but order is different. My requirement is to sort the data to have in alphabet order. That means both the above strings should look like
ABC-CDX-FFG
The incoming codes are unknown, they can be any 3 letter codes.
If anybody knows the logic please help me to sort the data in this field.
Thanks in advance |
|
Back to top |
|
|
Aaru
Senior Member
Joined: 03 Jul 2007 Posts: 1287 Location: Chennai, India
|
|
|
|
Kudamala,
Quote: |
My requirement is to sort the data to have in alphabet order. That means both the above strings should look like
ABC-CDX-FFG
|
What about the order of the records in the file? Should they be sorted? or you just want to sort the order (fields) in a record? |
|
Back to top |
|
|
kudamala
New User
Joined: 12 Sep 2008 Posts: 51 Location: Bangalore
|
|
|
|
Hi Aaru Vedam
I get the inputs from online into a field, I need to sort this field before storing into DB2. No file involved. Data comes from CICS input field and is stored in DB2. Before storing I need to sort this field
Thanks |
|
Back to top |
|
|
Escapa
Senior Member
Joined: 16 Feb 2007 Posts: 1399 Location: IL, USA
|
|
|
|
Ravi wrote: |
I have a requirement to sort the data in a string variable in COBOL.
|
Then why you have posted it in DFSORT forum?
If you need cobol solution you can use simple swap as below..
Code: |
DATA DIVISION.
WORKING-STORAGE SECTION.
01 INP PIC X(11) VALUE 'FFG-ABC-CDX'.
01 SWAP PIC X(3).
PROCEDURE DIVISION.
IF INP(1:3) > INP(5:3)
MOVE INP(1:3) TO SWAP
MOVE INP(5:3) TO INP(1:3)
MOVE SWAP TO INP(5:3)
END-IF
IF INP(5:3) > INP(9:3)
MOVE INP(5:3) TO SWAP
MOVE INP(9:3) TO INP(5:3)
MOVE SWAP TO INP(9:3)
END-IF
DISPLAY INP.
|
|
|
Back to top |
|
|
kudamala
New User
Joined: 12 Sep 2008 Posts: 51 Location: Bangalore
|
|
|
|
Thanks Sambhaji
Sorry I din't notice the forum.
I will try your solution and let you know
Once again thanks |
|
Back to top |
|
|
dbzTHEdinosauer
Global Moderator
Joined: 20 Oct 2006 Posts: 6966 Location: porcelain throne
|
|
|
|
Smbjhaji,
what if the input is CDX-FFG-ABC ? |
|
Back to top |
|
|
Escapa
Senior Member
Joined: 16 Feb 2007 Posts: 1399 Location: IL, USA
|
|
|
|
Dick Brenholtz wrote: |
Smbjhaji,
what if the input is CDX-FFG-ABC ?
|
Yeah.. I missed It..
Here it is corrected.
Code: |
IF INP(1:3) > INP(5:3)
MOVE INP(1:3) TO SWAP
MOVE INP(5:3) TO INP(1:3)
MOVE SWAP TO INP(5:3)
END-IF
IF INP(1:3) > INP(9:3)
MOVE INP(1:3) TO SWAP
MOVE INP(9:3) TO INP(1:3)
MOVE SWAP TO INP(9:3)
END-IF
IF INP(5:3) > INP(9:3)
MOVE INP(5:3) TO SWAP
MOVE INP(9:3) TO INP(5:3)
MOVE SWAP TO INP(9:3)
END-IF
DISPLAY INP.
|
|
|
Back to top |
|
|
dbzTHEdinosauer
Global Moderator
Joined: 20 Oct 2006 Posts: 6966 Location: porcelain throne
|
|
|
|
what if the input is FFG-CDX-ABC? |
|
Back to top |
|
|
Escapa
Senior Member
Joined: 16 Feb 2007 Posts: 1399 Location: IL, USA
|
|
|
|
Dick Brenholtz wrote: |
what if the input is FFG-CDX-ABC?
|
After First IF
CDX-FFG-ABC
After Second IF
ABC-FFG-CDX
After Third IF
ABC-CDX-FFG
Which is what required.... |
|
Back to top |
|
|
dbzTHEdinosauer
Global Moderator
Joined: 20 Oct 2006 Posts: 6966 Location: porcelain throne
|
|
|
|
good! |
|
Back to top |
|
|
kudamala
New User
Joined: 12 Sep 2008 Posts: 51 Location: Bangalore
|
|
|
|
Hello Dick
The value in the string is not fixed. approximately 100 three letter codes available for that field. It can be any combination of 3 letter codes of three codes delimited by hyphen.
I need to sort two fields one with 3 codes length and other with 7 codes length. Like below.
XYZ-DEC-AYZ----> FIELD1. after sort it should be AYZ-DEC-XYZ
ABC-DEC-PQR-CDX-TPC-MPN-AAA ----> FIELD2. after sort this shold be
AAA-ABC-CDX-DEC-PQR-MPN-TPC
These two fields handled individually.
I need to sort these two fields before storing them into DB2 database.
If you know the logic, how to sort these kind of data in Field, please help me.
Thanks in advance |
|
Back to top |
|
|
Escapa
Senior Member
Joined: 16 Feb 2007 Posts: 1399 Location: IL, USA
|
|
Back to top |
|
|
Escapa
Senior Member
Joined: 16 Feb 2007 Posts: 1399 Location: IL, USA
|
|
|
|
Quote: |
For eg I have a variable FIELD1 with a length of 11. where in this field the expected values are as follows:
|
Quote: |
The value in the string is not fixed. approximately 100 three letter codes available for that field. It can be any combination of 3 letter codes of three codes delimited by hyphen.
|
Initially you didn't tell this...
Then you need to use any sorting algorithm for it...
May be bubble(simpler one) sort..
One more doubt...
AAA-ABC-CDX-DEC-PQR-MPN-TPC
Why PQR ahead of MPN ?? |
|
Back to top |
|
|
kudamala
New User
Joined: 12 Sep 2008 Posts: 51 Location: Bangalore
|
|
|
|
Sambhaji
in AAA-ABC-CDX-DEC-PQR-MPN-TPC, you are right, MPN should come first.(human sort mistake )
The idea is to sort in alphabet order
Thanks |
|
Back to top |
|
|
Escapa
Senior Member
Joined: 16 Feb 2007 Posts: 1399 Location: IL, USA
|
|
|
|
Code: |
WORKING-STORAGE SECTION.
01 INP PIC X(27) VALUE 'ABC-DEC-PQR-CDX-TPC-MPN-AAA'.
01 SWAP PIC X(3).
01 WS-I PIC 99.
01 WS-J PIC 99.
PROCEDURE DIVISION.
PERFORM PARA1 VARYING WS-I FROM 1 BY 4 UNTIL WS-I > 25
DISPLAY INP.
STOP RUN.
PARA1.
PERFORM PARA2 VARYING WS-J FROM 1 BY 4 UNTIL WS-I - 4 < WS-J.
PARA2.
DISPLAY WS-I WS-J.
IF INP(WS-J:3) > INP(WS-I:3)
MOVE INP(WS-I:3) TO SWAP
MOVE INP(WS-J:3) TO INP(WS-I:3)
MOVE SWAP TO INP(WS-J:3)
END-IF.
|
|
|
Back to top |
|
|
kudamala
New User
Joined: 12 Sep 2008 Posts: 51 Location: Bangalore
|
|
|
|
Hello Sambhaji
Amazing....it works.....
Thank you so much.... |
|
Back to top |
|
|
Escapa
Senior Member
Joined: 16 Feb 2007 Posts: 1399 Location: IL, USA
|
|
|
|
Ravi wrote: |
Hello Sambhaji
Amazing....it works.....
Thank you so much....
|
You are Welcome. |
|
Back to top |
|
|
|