Portal | Manuals | References | Downloads | Info | Programs | JCLs | Master the Mainframes
IBM Mainframe Computers Forums Index
 
Register
 
IBM Mainframe Computers Forums Index Mainframe: Search IBM Mainframe Forum: FAQ Memberlist Usergroups Profile Log in to check your private messages Log in
 

 

How to sort the character groups in COBOL string variable

 
Post new topic   Reply to topic    IBMMAINFRAMES.com Support Forums -> COBOL Programming
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    Post subject: How to sort the character groups in COBOL string variable
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: 1288
Location: Chennai, India

PostPosted: Wed Jul 15, 2009 12:07 pm    Post subject: Reply to: How to sort the character groups in COBOL string v
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    Post subject:
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    Post subject:
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    Post subject:
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    Post subject:
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    Post subject:
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    Post subject:
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    Post subject:
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    Post subject:
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    Post subject:
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    Post subject:
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    Post subject:
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    Post subject:
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    Post subject:
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    Post subject:
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    Post subject:
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    IBMMAINFRAMES.com Support Forums -> COBOL Programming All times are GMT + 6 Hours
Page 1 of 1

 

Search our Forum:

Similar Topics
Topic Author Forum Replies Posted
No new posts Limit duplicate records in the SORT pshongal SYNCSORT 6 Mon Nov 21, 2016 12:54 pm
No new posts How to convert the VBM file to VB or... Sulabh Agrawal JCL & VSAM 4 Fri Nov 18, 2016 1:04 pm
This topic is locked: you cannot edit posts or make replies. RANDOM Function in COBOL swapnil781 COBOL Programming 2 Tue Nov 15, 2016 6:17 pm
No new posts Regarding COBOL Stored Procedure opti... selvamsrinivasan85 DB2 4 Fri Nov 04, 2016 8:57 pm
No new posts COBOL Version 6.1 Virendra Shambharkar COBOL Programming 5 Tue Nov 01, 2016 11:24 am


Facebook
Back to Top
 
Mainframe Wiki | Forum Rules | Bookmarks | Subscriptions | FAQ | Tutorials | Contact Us