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

How to sort an array in cobol


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

New User


Joined: 02 Jun 2008
Posts: 48
Location: India

PostPosted: Wed Mar 23, 2011 4:40 pm
Reply with quote

hi ,

In my requirement i am using an array which should get sort when i am loading data in to array.

but it is not loading into sorted order even my array declared like below


Code:

01  WT-RELN-ARRAY.                                         
    03  WT-RELN-TOT             PIC  S9(04) COMP VALUE 0.   
    03  WT-RELN-ROW             OCCURS 1 TO 1000           
                                DEPENDING ON WT-RELN-TOT   
                                ASCENDING KEY IS WT-RELN-CIN
                                INDEXED BY WT-RELN-IDX.     
       05 WT-RELN-CIN           PIC  X(10).                 


Can any body suggest me, how sort an array with out perform conditions
, is there any direct function to sort an array.
Back to top
View user's profile Send private message
Bill O'Boyle

CICS Moderator


Joined: 14 Jan 2008
Posts: 2501
Location: Atlanta, Georgia, USA

PostPosted: Wed Mar 23, 2011 4:56 pm
Reply with quote

Where are you getting each array-entry?

Are these entries already in sorted order? (apparently not icon_rolleyes.gif)

If not and you can't sort them using a COBOL Internal Sort, then you'd have to use a proven manual programmatic Sort method.

Try Googling SORT and you'll get millions of hits. Sort methods you may want to review (for example) are Bubble and Criss-Cross.

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

Global Moderator


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

PostPosted: Wed Mar 23, 2011 6:08 pm
Reply with quote

the ascending/descending clause is used to describe the condition of the data contained in the COBOL INTERNAL TABLE.
arrays in COBOL are the db2 definition of a COBOL Internal Table used for rowset selects.

as bill mentioned, either
  • pre-sort the data before input to the cobol module (yup, that means a sort step)
  • or write your own sort logic
Back to top
View user's profile Send private message
sant532

New User


Joined: 02 Jun 2008
Posts: 48
Location: India

PostPosted: Wed Mar 23, 2011 6:59 pm
Reply with quote

Then can any body suggest me, what is the use of declaring array like above ( ASCENDING KEY IS WT-RELN-CIN )


as per Dick explanation it wont sort even we declare with
ASCENDING KEY IS WT-RELN-CIN


is my understanding is correct.
Back to top
View user's profile Send private message
dbzTHEdinosauer

Global Moderator


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

PostPosted: Wed Mar 23, 2011 7:29 pm
Reply with quote

well, I will save you the problem of reading the manual.

a prerequisite for the SEARCH ALL (Binary search) instruction
is that the COBOL INTERNAL TABLE
must have either a descending or ascending clause and that the values contained be appropriately sorted.

now,
why would you want to use a SEACH ALL instead of SEARCH,
for that you will have to read the manual - or do a search.
Back to top
View user's profile Send private message
Robert Sample

Global Moderator


Joined: 06 Jun 2008
Posts: 8696
Location: Dubuque, Iowa, USA

PostPosted: Wed Mar 23, 2011 7:42 pm
Reply with quote

Your understanding is correct. COBOL will not sort the data in the array -- it expects you, the programmer, to ensure that the data is loaded into the table according to the key sequence. How you get it sorted is up to you -- SORT verb in COBOL, implementing a sort algorithm within your code, or even sorting the file via JCL before you read in the data. There are good reasons to use the ASCENDING (or DESCENDING) KEY phrase, but expecting COBOL to insert the data in sorted sequence is not one of those reasons.
Back to top
View user's profile Send private message
Kjeld

Active User


Joined: 15 Dec 2009
Posts: 365
Location: Denmark

PostPosted: Wed Mar 23, 2011 8:25 pm
Reply with quote

If the key values of your array set is contiguos you might even be able to calculate the index value for the array. But the again, then you do not have to search the array: You only need to calculate the index value again to reference the wanted element when you do lookup..
Back to top
View user's profile Send private message
Bill O'Boyle

CICS Moderator


Joined: 14 Jan 2008
Posts: 2501
Location: Atlanta, Georgia, USA

PostPosted: Wed Mar 23, 2011 8:35 pm
Reply with quote

Are you sure 1000 is a good maximum for your OCCURS DEPENDING ON array?

If you are loading the array from a SORTED file and the file is sorted prior to invoking your program, could you pass the number of records to your program as a parm?

You would then know immediately if the maximum occurs of 1000 will be enough.

Note that although the array is an ODO and the fact that it's defined to WS, the amount of storage allocated will still be (1000 * 10), regardless.

Have you considered moving the array to LINKAGE and obtain only the amount of storage you need, based upon the number of records in the file and allocate this storage, using the LE Callable Service routine "CEEGTST". This is referred to as "Dynamic Storage Allocation".

If so, you can increase the maximum in the ODO to a very large number, but the actual storage acquired will be a calculation. This will prevent you not getting a call in the middle of the night, because the 1000 maximum in your ODO WS array is too small.

ODO arrays in WS as opposed to LINKAGE are treated and allocated differently.

Something to consider....

Bill
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 COBOL -Linkage Section-Case Sensitive COBOL Programming 1
No new posts COBOL ZOS Web Enablement Toolkit HTTP... COBOL Programming 0
Search our Forums:

Back to Top