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

Sort a internal array (table) in COBOL


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

New User


Joined: 20 Jan 2007
Posts: 6
Location: India

PostPosted: Thu Nov 01, 2007 7:48 pm
Reply with quote

Hi All,

We have a requirement in which we cant go in for SORT PROCEDURE in COBOL using sort work files.

Is there any way to sort the records in the internal table in A/D order based on the key of certain bytes in the records.

Requirement is to similar to the Sort card we use for SYNCSORT.

Thanks in Advance.

Regards
Back to top
View user's profile Send private message
CICS Guy

Senior Member


Joined: 18 Jul 2007
Posts: 2146
Location: At my coffee table

PostPosted: Thu Nov 01, 2007 8:15 pm
Reply with quote

I don't know about your "sort card", but you can internally sort tables in various ways, give a look at this Bubble Sort......
Back to top
View user's profile Send private message
dbzTHEdinosauer

Global Moderator


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

PostPosted: Fri Nov 02, 2007 2:46 pm
Reply with quote

cheethra, I would suggest you start with the Bubble Sort link provided by CICS_Guy.

you will always need a third area/field to save one of the items during the switch of the lesser and the greater.
Back to top
View user's profile Send private message
revel

Active User


Joined: 05 Apr 2005
Posts: 135
Location: Bangalore/Chennai-INDIA

PostPosted: Fri Nov 02, 2007 2:57 pm
Reply with quote

Small correction,

Use below code for SORTING

---------------------------------------

WORKING STORAGE SECTION.
01 WS-NUMBERS.
10 WS-NUM PIC 999 OCCURS 10 TIMES.
01 WS-I PIC 99.
01 WS-J PIC 99.
01 WS-K PIC 99.
01 WS-TEMP PIC 999.

PROCEDURE DIVISION.
0000-MAIN.
PERFORM 1000-READ VARYING WS-I FROM 1 BY 1 UNTIL WS-I > 10.
1000-READ.
COMPUTE WS-J = WS-I + 1.
PERFORM 2000-COMPARE
VARYING WS-K FROM WS-J BY 1 UNTIL WS-K >= 10.
2000-COMPARE.
*********************************************************
* LOGIC FOR ORDERING NUMBERS IN ASC
*********************************************************
IF WS-NUM(WS-I) > WS-NUM(WS-K)
MOVE WS-NUM(WS-I) TO WS-TEMP
MOVE WS-NUM(WS-K) TO WS-NUM(WS-I)
MOVE WS-TEMP TO WS-NUM(WS-K)
ELSE
CONTINUE
END-IF.
.
.
--------------------------------------------------------------------------
--------------------------------------------------------------------------

2000-COMPARE.
*********************************************************
* LOGIC FOR ORDERING NUMBERS IN DESC
*********************************************************
IF WS-NUM(WS-I) > WS-NUM(WS-K)
CONTINUE
ELSE
MOVE WS-NUM(WS-I) TO WS-TEMP
MOVE WS-NUM(WS-K) TO WS-NUM(WS-I)
MOVE WS-TEMP TO WS-NUM(WS-K)
END-IF.

.
.


Use above pseudo code....


Regards,
Raghavendra icon_razz.gif
Back to top
View user's profile Send private message
cheethra

New User


Joined: 20 Jan 2007
Posts: 6
Location: India

PostPosted: Fri Nov 02, 2007 3:51 pm
Reply with quote

Hi All,

Thanks for your replies.

Let me explain what i wanted to do.

Assume there are five records of length 80 in a table, i wanted to pick few fields from each record and form a key, with this key i wanted to sort the records.

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

Active User


Joined: 05 Apr 2005
Posts: 135
Location: Bangalore/Chennai-INDIA

PostPosted: Fri Nov 02, 2007 4:41 pm
Reply with quote

cheethra wrote:


Assume there are five records of length 80 in a table, i wanted to pick few fields from each record and form a key, with this key i wanted to sort the records.




Cheetra,

We can use same logic which i gaven before provided the fields should be present in same columns(Key fields)

ex;

AAAAAAAAAAAA1234BBBBBB0000CCCCCCC
AAAAAAAAAAAA2344BBBBBB1111CCCCCCC
AAAAAAAAAAAA2345BBBBBB3333CCCCCCC

The Keys in the above example are

1234 and 0000 in first row
2344 and 1111 in second row
2345 and 3333 in third row

Use Referance modification concept for this

Regard's
Raghavendra
Back to top
View user's profile Send private message
dbzTHEdinosauer

Global Moderator


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

PostPosted: Fri Nov 02, 2007 9:51 pm
Reply with quote

Quote:

Use Referance modification concept for this


If you want to write assembler, write assembler.

But if you are writing COBOL, properly define your data in working storage.

If indeed the keys fields in the OP's record are not contiguous, the IF statements for multiple reference modifications will be difficult to read, understand and debug.

sub-define your item properly and refer to the key fields as data elements without the reference modification so that someone else can read your code and understand what is/was being done.
Back to top
View user's profile Send private message
dick scherrer

Moderator Emeritus


Joined: 23 Nov 2006
Posts: 19244
Location: Inside the Matrix

PostPosted: Fri Nov 02, 2007 11:19 pm
Reply with quote

Hello,

FWIW, it is "your" code and "your" ws definitions. You can control how it is implemented.

If this was my requirement, i'd add my own first field (group with the "key" components beneath it) and make it the "key". Sorting and searching would both be much easier to understand and work with.

Again, this is just IMHO.
Back to top
View user's profile Send private message
Craq Giegerich

Senior Member


Joined: 19 May 2007
Posts: 1512
Location: Virginia, USA

PostPosted: Fri Nov 02, 2007 11:31 pm
Reply with quote

Mixing reference modification with subscripting/indexing gets very hard to read. Add it the complex IF/OR/AND that is needed to handle a multiple part key and it will end an unreadable/unmaintainable mess.

Which comes first with a subscripted field with reference modification the subscript or the reference modification?
Back to top
View user's profile Send private message
CICS Guy

Senior Member


Joined: 18 Jul 2007
Posts: 2146
Location: At my coffee table

PostPosted: Fri Nov 02, 2007 11:51 pm
Reply with quote

Craq Giegerich wrote:
Which comes first with a subscripted field with reference modification the subscript or the reference modification?
I can never remember, but the compiler will let you know if you have it backward..... icon_wink.gif
Back to top
View user's profile Send private message
dick scherrer

Moderator Emeritus


Joined: 23 Nov 2006
Posts: 19244
Location: Inside the Matrix

PostPosted: Sat Nov 03, 2007 12:00 am
Reply with quote

Hello,

Quote:
Which comes first with a subscripted field with reference modification the subscript or the reference modification?
Subscript first, then reference modification. I.e.
Code:
MOVE DTL-RX-REFILL(1) (2:1) TO RE-NO.
Back to top
View user's profile Send private message
CICS Guy

Senior Member


Joined: 18 Jul 2007
Posts: 2146
Location: At my coffee table

PostPosted: Sat Nov 03, 2007 1:08 am
Reply with quote

And the duplicate name qualifier?..... icon_lol.gif
Back to top
View user's profile Send private message
dick scherrer

Moderator Emeritus


Joined: 23 Nov 2006
Posts: 19244
Location: Inside the Matrix

PostPosted: Sat Nov 03, 2007 1:17 am
Reply with quote

As you mentioned some time ago. . . .

"We don' need no steenkin'" duplicate names. . . icon_smile.gif
Back to top
View user's profile Send private message
revel

Active User


Joined: 05 Apr 2005
Posts: 135
Location: Bangalore/Chennai-INDIA

PostPosted: Mon Nov 05, 2007 9:54 am
Reply with quote

Hi all,

Just to add thoughts,

As per my knowledge, we can approach in other way also.

Consider a table having structure

Quote:
AAAAAAAAAAAA1234BBBBBB0000CCCCCCC
AAAAAAAAAAAA2344BBBBBB1111CCCCCCC
AAAAAAAAAAAA2345BBBBBB3333CCCCCCC


Declare in Working storage section as a Group items

say

01 ws-table.
05 ws-tab occurs 10 times.
10 ws-a pic x(10).
10 ws-b pic x(20). <-- Let it be Key>
10 ws-c pic x(30).
10 ws-d pic x(10).
<----- Let it be another Key>

Use declared items as array type

Say IF WS-B(1) .....
.....


Any suggestionis welcomed

Regards,
Raghavendra
Back to top
View user's profile Send private message
dick scherrer

Moderator Emeritus


Joined: 23 Nov 2006
Posts: 19244
Location: Inside the Matrix

PostPosted: Mon Nov 05, 2007 8:22 pm
Reply with quote

Hello,

Quote:
Say IF WS-B(1) .....
This will still require "sorting" and "searching" on a multiple field "key". As has been mentioned previously, working with the compound key increases the amount of work needed.

To clarify, idf this were my requirement, i would define the array with a multi-field (group) key and have all of the detail fields named in the array. This way i could work with somple compares and not need to use reference modification. As was also mentioned, combining array processing and reference modification will tent to make the code harder to read/maintain.
Back to top
View user's profile Send private message
CICS Guy

Senior Member


Joined: 18 Jul 2007
Posts: 2146
Location: At my coffee table

PostPosted: Mon Nov 05, 2007 8:41 pm
Reply with quote

cheethra wrote:
We have a requirement in which we cant go in for SORT PROCEDURE in COBOL using sort work files.
Using an internal call to Syncsort for that few records does not require 'sort work files'...
Quote:
Requirement is to similar to the Sort card we use for SYNCSORT.
Since the Assembler to Syncsort call looks standard, I'd bet you could call it from COBOL and yes, you could point to the sort cards you use for Syncsort....
A rather long trip for a half dozen records......
Back to top
View user's profile Send private message
revel

Active User


Joined: 05 Apr 2005
Posts: 135
Location: Bangalore/Chennai-INDIA

PostPosted: Tue Nov 06, 2007 10:03 am
Reply with quote

Dear DICK,

Quote:
working with the compound key increases the amount of work needed


Could you explain me how amount of work will increasing

<As per my knowledge, MAIN MEMORY will contribute space for to store ARRAY-Internal array. Either, we declare Data item as a type of GROUP ITEM or As a type of INDEPENDENT ITEM(Variable)>

More over requirement is like that

Quote:
Assume there are five records of length 80 in a table, i wanted to pick few fields from each record and form a key, with this key i wanted to sort the records.


Just i am giving approach

This is just for clarification

after all we are learner, right.
Back to top
View user's profile Send private message
dick scherrer

Moderator Emeritus


Joined: 23 Nov 2006
Posts: 19244
Location: Inside the Matrix

PostPosted: Tue Nov 06, 2007 10:20 pm
Reply with quote

Hello,

Quote:
Could you explain me how amount of work will increasing
"Sorting" the table/array and searching the array will be more complex using multiple fields for "the key" - hence more work.

It may also lead to longer development time and errors depending on the skill of the coder.
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 Load new table with Old unload - DB2 DB2 6
No new posts COBOL -Linkage Section-Case Sensitive COBOL Programming 1
No new posts COBOL ZOS Web Enablement Toolkit HTTP... COBOL Programming 0
No new posts Pulling a fixed number of records fro... DB2 2
No new posts JCL sort card - get first day and las... JCL & VSAM 9
Search our Forums:

Back to Top