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 an array in cobol

 
Post new topic   Reply to topic    IBMMAINFRAMES.com Support Forums -> COBOL Programming
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    Post subject: How to sort an array in cobol
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: 2502
Location: Atlanta, Georgia, USA

PostPosted: Wed Mar 23, 2011 4:56 pm    Post subject: Reply to: How to sort an array in cobol
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: 6968
Location: porcelain throne

PostPosted: Wed Mar 23, 2011 6:08 pm    Post subject:
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    Post subject:
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: 6968
Location: porcelain throne

PostPosted: Wed Mar 23, 2011 7:29 pm    Post subject:
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: 8117
Location: East Dubuque, Illinois, USA

PostPosted: Wed Mar 23, 2011 7:42 pm    Post subject:
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    Post subject:
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: 2502
Location: Atlanta, Georgia, USA

PostPosted: Wed Mar 23, 2011 8:35 pm    Post subject: Reply to: How to sort an array in cobol
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    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 XML Parse for COBOL 5.2 Bhanu Praveen COBOL Programming 1 Sat Jul 08, 2017 8:58 pm
No new posts SORT to unpack a feild shr_amar DFSORT/ICETOOL 7 Fri Jul 07, 2017 4:32 am
No new posts How to write Specific Fields from Mul... Padhu SYNCSORT 6 Thu Jul 06, 2017 10:26 am
No new posts Optimize sort inrec parse vishwakotin DFSORT/ICETOOL 6 Mon Jun 26, 2017 11:15 pm
No new posts Dynamic output file creation in cobol... smileheal COBOL Programming 7 Thu Jun 15, 2017 10:53 pm


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