Array concatenation: Difference between revisions

Content added Content deleted
(→‎{{header|Zig}}: fix, add deinit() where appropriate, use std.mem.concat)
imported>Acediast
(→‎{{header|COBOL}}: specified version)
Line 1,210: Line 1,210:


=={{header|COBOL}}==
=={{header|COBOL}}==
{{works with|COBOL 2014}}
<syntaxhighlight lang="cobol"> identification division.
<syntaxhighlight lang="cobolfree">IDENTIFICATION DIVISION.
program-id. array-concat.
PROGRAM-ID. array-concat.


DATA DIVISION.
environment division.
WORKING-STORAGE SECTION.
configuration section.
01 table-one.
repository.
05 int-field PIC 999 OCCURS 0 TO 5 TIMES DEPENDING ON t1.
function all intrinsic.
01 table-two.
05 int-field PIC 9(4) OCCURS 0 TO 10 TIMES DEPENDING ON t2.
77 tally USAGE IS INDEX.
77 t1 PIC 99.
77 t2 PIC 99.
77 show PIC Z(4) USAGE IS DISPLAY.


PROCEDURE DIVISION.
data division.
array-concat-main.
working-storage section.
01 table-one.
PERFORM initialize-tables
PERFORM concatenate-tables
05 int-field pic 999 occurs 0 to 5 depending on t1.
01 table-two.
PERFORM display-result
GOBACK.
05 int-field pic 9(4) occurs 0 to 10 depending on t2.


initialize-tables.
77 t1 pic 99.
MOVE 4 TO t1
77 t2 pic 99.
PERFORM VARYING tally FROM 1 BY 1 UNTIL tally > t1
COMPUTE int-field OF table-one(tally) = tally * 3
END-PERFORM
MOVE 3 TO t2
PERFORM VARYING tally FROM 1 BY 1 UNTIL tally > t2
COMPUTE int-field OF table-two(tally) = tally * 6
END-PERFORM.


concatenate-tables.
77 show pic z(4).
PERFORM VARYING tally FROM 1 BY 1 UNTIL tally > t1
ADD 1 TO t2
MOVE int-field OF table-one(tally)
TO int-field OF table-two(t2)
END-PERFORM.


display-result.
procedure division.
PERFORM VARYING tally FROM 1 BY 1 UNTIL tally = t2
array-concat-main.
MOVE int-field OF table-two(tally) TO show
perform initialize-tables
DISPLAY FUNCTION TRIM(show) ", " WITH NO ADVANCING
perform concatenate-tables
END-PERFORM
perform display-result
MOVE int-field OF table-two(tally) TO show
goback.
DISPLAY FUNCTION TRIM(show).


END PROGRAM array-concat.</syntaxhighlight>
initialize-tables.
move 4 to t1
perform varying tally from 1 by 1 until tally > t1
compute int-field of table-one(tally) = tally * 3
end-perform

move 3 to t2
perform varying tally from 1 by 1 until tally > t2
compute int-field of table-two(tally) = tally * 6
end-perform
.

concatenate-tables.
perform varying tally from 1 by 1 until tally > t1
add 1 to t2
move int-field of table-one(tally)
to int-field of table-two(t2)
end-perform
.

display-result.
perform varying tally from 1 by 1 until tally = t2
move int-field of table-two(tally) to show
display trim(show) ", " with no advancing
end-perform
move int-field of table-two(tally) to show
display trim(show)
.

end program array-concat.</syntaxhighlight>
{{out}}
{{out}}
<pre>prompt$ cobc -xjd array-concatenation.cob
<pre>$ cobc -xjd array-concatenation.cob --std=cobol2014 # COBOL 2014 needed for FUNCTION TRIM
6, 12, 18, 3, 6, 9, 12
6, 12, 18, 3, 6, 9, 12
</pre>
</pre>