IBM Mainframe Forum Index
 
Log In
 
IBM Mainframe Forum Index Mainframe: Search IBM Mainframe Forum: FAQ Register
 

How to sort the character groups in COBOL string variable


IBM Mainframe Forums -> COBOL Programming
Post new topic   Reply to topic
View previous topic :: View next topic  
Author Message
kudamala

New User


Joined: 12 Sep 2008
Posts: 51
Location: Bangalore

PostPosted: Wed Jul 15, 2009 11:54 am
Reply with quote

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
View user's profile Send private message
Aaru

Senior Member


Joined: 03 Jul 2007
Posts: 1287
Location: Chennai, India

PostPosted: Wed Jul 15, 2009 12:07 pm
Reply with quote

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
View user's profile Send private message
kudamala

New User


Joined: 12 Sep 2008
Posts: 51
Location: Bangalore

PostPosted: Wed Jul 15, 2009 1:58 pm
Reply with quote

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
View user's profile Send private message
Escapa

Senior Member


Joined: 16 Feb 2007
Posts: 1399
Location: IL, USA

PostPosted: Wed Jul 15, 2009 2:24 pm
Reply with quote

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
View user's profile Send private message
kudamala

New User


Joined: 12 Sep 2008
Posts: 51
Location: Bangalore

PostPosted: Wed Jul 15, 2009 3:55 pm
Reply with quote

Thanks Sambhaji

Sorry I din't notice the forum.
I will try your solution and let you know

Once again thanks
Back to top
View user's profile Send private message
dbzTHEdinosauer

Global Moderator


Joined: 20 Oct 2006
Posts: 6966
Location: porcelain throne

PostPosted: Wed Jul 15, 2009 4:21 pm
Reply with quote

Smbjhaji,

what if the input is CDX-FFG-ABC ?
Back to top
View user's profile Send private message
Escapa

Senior Member


Joined: 16 Feb 2007
Posts: 1399
Location: IL, USA

PostPosted: Wed Jul 15, 2009 5:23 pm
Reply with quote

Dick Brenholtz wrote:

Smbjhaji,

what if the input is CDX-FFG-ABC ?


Yeah.. I missed It.. icon_sad.gif

Here it is corrected. icon_smile.gif
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
View user's profile Send private message
dbzTHEdinosauer

Global Moderator


Joined: 20 Oct 2006
Posts: 6966
Location: porcelain throne

PostPosted: Wed Jul 15, 2009 5:36 pm
Reply with quote

what if the input is FFG-CDX-ABC?
Back to top
View user's profile Send private message
Escapa

Senior Member


Joined: 16 Feb 2007
Posts: 1399
Location: IL, USA

PostPosted: Wed Jul 15, 2009 5:53 pm
Reply with quote

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
View user's profile Send private message
dbzTHEdinosauer

Global Moderator


Joined: 20 Oct 2006
Posts: 6966
Location: porcelain throne

PostPosted: Wed Jul 15, 2009 6:48 pm
Reply with quote

good!
Back to top
View user's profile Send private message
kudamala

New User


Joined: 12 Sep 2008
Posts: 51
Location: Bangalore

PostPosted: Thu Jul 16, 2009 10:27 am
Reply with quote

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
View user's profile Send private message
Escapa

Senior Member


Joined: 16 Feb 2007
Posts: 1399
Location: IL, USA

PostPosted: Thu Jul 16, 2009 10:29 am
Reply with quote

Quote:
good!

icon_biggrin.gif
Back to top
View user's profile Send private message
Escapa

Senior Member


Joined: 16 Feb 2007
Posts: 1399
Location: IL, USA

PostPosted: Thu Jul 16, 2009 10:34 am
Reply with quote

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... icon_question.gif

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
View user's profile Send private message
kudamala

New User


Joined: 12 Sep 2008
Posts: 51
Location: Bangalore

PostPosted: Thu Jul 16, 2009 10:43 am
Reply with quote

Sambhaji

in AAA-ABC-CDX-DEC-PQR-MPN-TPC, you are right, MPN should come first.(human sort mistake icon_razz.gif )

The idea is to sort in alphabet order

Thanks
Back to top
View user's profile Send private message
Escapa

Senior Member


Joined: 16 Feb 2007
Posts: 1399
Location: IL, USA

PostPosted: Thu Jul 16, 2009 12:51 pm
Reply with quote

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
View user's profile Send private message
kudamala

New User


Joined: 12 Sep 2008
Posts: 51
Location: Bangalore

PostPosted: Thu Jul 16, 2009 3:15 pm
Reply with quote

Hello Sambhaji

Amazing....it works.....

Thank you so much....
Back to top
View user's profile Send private message
Escapa

Senior Member


Joined: 16 Feb 2007
Posts: 1399
Location: IL, USA

PostPosted: Thu Jul 16, 2009 3:58 pm
Reply with quote

Ravi wrote:

Hello Sambhaji

Amazing....it works.....

Thank you so much....


You are Welcome. icon_smile.gif
Back to top
View user's profile Send private message
View previous topic :: :: View next topic  
Post new topic   Reply to topic View Bookmarks
All times are GMT + 6 Hours
Forum Index -> COBOL Programming

 


Similar Topics
Topic Forum Replies
No new posts Need to set RC4 through JCL SORT DFSORT/ICETOOL 5
No new posts How to split large record length file... DFSORT/ICETOOL 10
No new posts Replace each space in cobol string wi... COBOL Programming 3
No new posts PARSE Syntax for not fix length word ... JCL & VSAM 7
No new posts Extracting Variable decimal numbers f... DFSORT/ICETOOL 17
Search our Forums:

Back to Top