View previous topic :: View next topic
|
Author |
Message |
sri.mainframes
New User
Joined: 16 Feb 2006 Posts: 29 Location: MUMBAI
|
|
|
|
I have two arrays , A & B .
A holds alphanumeric data that may or may not contain duplicates
if duplicate encouter we have to sum corresponding values in B.,
B holds numeric data
for ex let us suppose A and B having the following values
A B
------- -----
abc 2
xyz 1
abc 1
mno 4
abc 1
abc 1
xyz 2
mno 1
stu 2
then i want to display them as
A B
------- -----
abc 5
xyz 3
mno 5
stu 2
can anyone help me to solve this problem.
thanks in advance
sri.prince |
|
Back to top |
|
|
DavidatK
Active Member
Joined: 22 Nov 2005 Posts: 700 Location: Troy, Michigan USA
|
|
|
|
sri.prince,
How many entries do you expect to have in the first array, and how large are each entry?
You can always use the COBOL SORT to do this, but I will do almost anything not to use this, it's very resource intensive. I would probably write a bubble sort then sum the results.
Dave |
|
Back to top |
|
|
martin9
Active User
Joined: 01 Mar 2006 Posts: 290 Location: Basel, Switzerland
|
|
|
|
hy sri.mainframes,
this is not very difficult to do.
first question, if array A holds data as an identifier (alphachar)
and array B holds numeric data, how you want to join them?
probably your example is not complete.
Array A:
abc 2
xyz 1
abc 1
mno 4
abc 1
abc 1
xyz 2
mno 1
stu 2
Array B:
the sums of array A...
--> correct me if i'm wrong...
martin9 |
|
Back to top |
|
|
sri.mainframes
New User
Joined: 16 Feb 2006 Posts: 29 Location: MUMBAI
|
|
|
|
Hi Martin
I dont want to join them .
Size of the Arrays A & B does not matter but each data item have the same length i.e., pic A(8) and B is pic 9(3)
A B
------- -----
abc 2
xyz 1
abc 1
mno 4
abc 1
abc 1
xyz 2
mno 1
stu 2
011 2
011 1
then the required outcome should be like this
A B
------- -----
abc 5
xyz 3
mno 5
stu 2
011 3
try this one and help me out
thanks
sri.mainframes |
|
Back to top |
|
|
DavidatK
Active Member
Joined: 22 Nov 2005 Posts: 700 Location: Troy, Michigan USA
|
|
|
|
sri.mainframes,
Try this code
Code: |
WORKING-STORAGE SECTION.
01 TABLES.
05 A-VALUES.
10 FILLER PIC X(8) VALUE 'ABC'.
10 FILLER PIC X(8) VALUE 'XYZ'.
10 FILLER PIC X(8) VALUE 'ABC'.
10 FILLER PIC X(8) VALUE 'MNO'.
10 FILLER PIC X(8) VALUE 'ABC'.
10 FILLER PIC X(8) VALUE 'ABC'.
10 FILLER PIC X(8) VALUE 'XYZ'.
10 FILLER PIC X(8) VALUE 'MNO'.
10 FILLER PIC X(8) VALUE 'STU'.
10 FILLER PIC X(8) VALUE '011'.
10 FILLER PIC X(8) VALUE '011'.
05 A REDEFINES A-VALUES PIC X(8) OCCURS 11 TIMES.
05 B-VALUES.
10 FILLER PIC 9(3) VALUE 2.
10 FILLER PIC 9(3) VALUE 1.
10 FILLER PIC 9(3) VALUE 1.
10 FILLER PIC 9(3) VALUE 4.
10 FILLER PIC 9(3) VALUE 1.
10 FILLER PIC 9(3) VALUE 1.
10 FILLER PIC 9(3) VALUE 2.
10 FILLER PIC 9(3) VALUE 1.
10 FILLER PIC 9(3) VALUE 2.
10 FILLER PIC 9(3) VALUE 2.
10 FILLER PIC 9(3) VALUE 1.
05 B REDEFINES B-VALUES PIC 9(3) OCCURS 11 TIMES.
05 SUB-1 PIC S9(3) COMP-3
VALUE 1.
05 SUB-2 PIC S9(3) COMP-3
VALUE 1.
LINKAGE SECTION.
PROCEDURE DIVISION.
DISPLAY 'BEFORE SUM'.
DISPLAY ' '.
PERFORM
VARYING SUB-1 FROM 1 BY 1
UNTIL SUB-1 > 11
DISPLAY A(SUB-1) ' ' B(SUB-1)
END-PERFORM.
PERFORM
VARYING SUB-1 FROM 1 BY 1
UNTIL SUB-1 > 11
PERFORM
VARYING SUB-2 FROM 1 BY 1
UNTIL SUB-2 > SUB-1 - 1
IF A(SUB-1) = A(SUB-2)
THEN
ADD B(SUB-1) TO B(SUB-2)
MOVE 0 TO B(SUB-1)
MOVE SPACES TO A(SUB-1)
ELSE
IF A(SUB-2) = SPACES
THEN
MOVE A(SUB-1) TO A(SUB-2)
MOVE B(SUB-1) TO B(SUB-2)
MOVE SPACES TO A(SUB-1)
MOVE 0 TO B(SUB-1)
END-IF
END-IF
END-PERFORM
END-PERFORM.
DISPLAY ' '.
DISPLAY 'AFTER SUM'.
DISPLAY ' '.
PERFORM
VARYING SUB-1 FROM 1 BY 1
UNTIL SUB-1 > 11
DISPLAY A(SUB-1) ' ' B(SUB-1)
END-PERFORM.
GOBACK.
|
Results of running program:
Code: |
.
.BEFORE SUM
.
.ABC 002
.XYZ 001
.ABC 001
.MNO 004
.ABC 001
.ABC 001
.XYZ 002
.MNO 001
.STU 002
.011 002
.011 001
.
.AFTER SUM
.
.ABC 005
.XYZ 003
.MNO 005
.STU 002
.011 003
. 000
. 000
. 000
. 000
. 000
. 000
|
Dave |
|
Back to top |
|
|
sri.mainframes
New User
Joined: 16 Feb 2006 Posts: 29 Location: MUMBAI
|
|
|
|
Hi Davidatk,
thanks for your help
sri.prince |
|
Back to top |
|
|
|