Array concatenation: Difference between revisions

m
(23 intermediate revisions by 15 users not shown)
Line 902:
=={{header|BASIC}}==
==={{header|Applesoft BASIC}}===
{{works with|Chipmunk Basic}}
<syntaxhighlight lang="gwbasic"> 10 LET X = 4:Y = 5
20 DIM A(X - 1),B(Y - 1),C(X + Y - 1)
Line 909 ⟶ 910:
60 FOR I = 1 TO Y:C(X + I - 1) = B(I - 1): NEXT
70 FOR I = 1 TO X + Y: PRINT MID$ (" ",1,I > 1)C(I - 1);: NEXT</syntaxhighlight>
 
==={{header|Chipmunk Basic}}===
{{works with|Chipmunk Basic|3.6.4}}
The [[#GW-BASIC|GW-BASIC]] solution works without any changes.
 
==={{header|GW-BASIC}}===
{{works with|PC-BASIC|any}}
{{works with|BASICA}}
{{works with|Chipmunk Basic}}
{{works with|QBasic}}
{{works with|MSX BASIC}}
<syntaxhighlight lang="qbasic">100 U1 = 3: U2 = 4
110 DIM A$(3)
120 DATA "The","quick","brown","fox"
130 FOR I = 0 TO U1 : READ A$(I) : NEXT I
140 DIM B$(4)
150 DATA "jumped","over","the","lazy","dog"
160 FOR I = 0 TO U2 : READ B$(I) : NEXT I
170 'SU2 ConcatArrays
180 X = U1 + 1
190 Y = U2 + 1
200 Z = X + Y
210 DIM C$(Z-1)
220 FOR I = 0 TO X-1
230 C$(I) = A$(I)
240 NEXT I
250 FOR I = 0 TO Y-1
260 C$(U1+I+1) = B$(I)
270 NEXT I
280 '
290 FOR I = 0 TO Z-1
300 PRINT C$(I); " ";
310 NEXT I
320 END</syntaxhighlight>
 
==={{header|Minimal BASIC}}===
{{trans|Applesoft BASIC}}
{{works with|GW-BASIC}}
{{works with|Chipmunk Basic}}
{{works with|QBasic}}
{{works with|Quite BASIC}}
{{works with|MSX BASIC}}
<syntaxhighlight lang="qbasic">10 LET X = 4
20 LET Y = 5
30 DIM A(3)
40 DIM B(4)
50 DIM C(8)
60 FOR I = 1 TO X
70 LET A(I-1) = I
80 NEXT I
90 FOR I = 1 TO Y
100 LET B(I-1) = I*10
110 NEXT I
120 FOR I = 1 TO X
130 LET C(I-1) = A(I-1)
140 NEXT I
150 FOR I = 1 TO Y
160 LET C(X+I-1) = B(I-1)
170 NEXT I
180 FOR I = 1 TO X+Y
190 PRINT C(I-1);
200 NEXT I
210 END</syntaxhighlight>
 
==={{header|MSX Basic}}===
{{works with|MSX BASIC|any}}
The [[#GW-BASIC|GW-BASIC]] solution works without any changes.
 
==={{header|Quite BASIC}}===
{{trans|GW-BASIC}}
<syntaxhighlight lang="qbasic">100 LET U1 = 3
105 LET U2 = 4
110 ARRAY A$
120 DATA "The","quick","brown","fox"
130 FOR I = 0 TO U1 : READ A$(I) : NEXT I
140 ARRAY B$
150 DATA "jumped","over","the","lazy","dog"
160 FOR I = 0 TO U2 : READ B$(I) : NEXT I
170 rem Sub ConcatArrays
180 LET X = U1 + 1
190 LET Y = U2 + 1
200 LET Z = X + Y
210 ARRAY C
220 FOR I = 0 TO X-1
230 LET C$(I) = A$(I)
240 NEXT I
250 FOR I = 0 TO Y-1
260 LET C$(U1 + I + 1) = B$(I)
270 NEXT I
280 rem
290 FOR I = 0 TO Z-1
300 PRINT C$(I);" ";
310 NEXT I
320 END</syntaxhighlight>
 
==={{header|BaCon}}===
<syntaxhighlight lang="bacon">DECLARE a[] = { 1, 2, 3, 4, 5 }
Line 961 ⟶ 1,057:
160 : PRINT C(I);
170 NEXT</syntaxhighlight>
 
==={{header|Run BASIC}}===
{{works with|Just BASIC}}
{{works with|Liberty BASIC}}
The [[#Liberty BASIC|Liberty BASIC]] solution works without any changes.
 
=={{header|BASIC256}}==
Line 1,002 ⟶ 1,103:
<pre>1, 2, 3, 4, 5, 6, 7, 8, 9, 10</pre>
 
=={{header|Binary Lambda Calculus}}==
 
BLC uses lists instead of arrays. List concatenation is (see also https://github.com/tromp/AIT/blob/master/lists/cat.lam)
 
<pre>00011001000110100000000000010110111100101111001111110111110110</pre>
 
=={{header|BQN}}==
Line 1,210 ⟶ 1,316:
 
=={{header|COBOL}}==
{{works with|COBOL 2014}}
<syntaxhighlight lang="cobol"> identification division.
<syntaxhighlight lang="cobolfree">IDENTIFICATION DIVISION.
program-id. array-concat.
PROGRAM-ID. array-concat.
 
environment division.
configuration section.
repository.
function all intrinsic.
 
data division.
working-storage section.
01 table-one.
05 int-field pic 999 occurs 0 to 5 depending on t1.
01 table-two.
05 int-field pic 9(4) occurs 0 to 10 depending on t2.
 
77 t1 pic 99.
77 t2 pic 99.
 
77 show pic z(4).
 
DATA DIVISION.
procedure division.
WORKING-STORAGE SECTION.
array-concat-main.
01 table-one.
perform initialize-tables
05 int-field PIC 999 OCCURS 0 TO 5 TIMES DEPENDING ON t1.
perform concatenate-tables
01 table-two.
perform display-result
05 int-field PIC 9(4) OCCURS 0 TO 10 TIMES DEPENDING ON t2.
goback.
77 tally USAGE IS INDEX.
77 t1 PIC 99.
77 t2 PIC 99.
77 show PIC Z(4) USAGE IS DISPLAY.
 
PROCEDURE DIVISION.
initialize-tables.
array-concat-main.
move 4 to t1
PERFORM initialize-tables
perform varying tally from 1 by 1 until tally > t1
PERFORM concatenate-tables
compute int-field of table-one(tally) = tally * 3
PERFORM enddisplay-performresult
GOBACK.
 
initialize-tables.
move 3 to t2
MOVE 4 TO t1
perform varying tally from 1 by 1 until tally > t2
PERFORM VARYING tally FROM 1 BY 1 compute int-field of table-two(tally) =UNTIL tally *> 6t1
COMPUTE int-field OF endtable-performone(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 perform varyingVARYING tally fromFROM 1 byBY 1 untilUNTIL tally > t1
addADD 1 toTO t2
moveMOVE int-field ofOF table-one(tally)
toTO int-field ofOF table-two(t2)
endEND-performPERFORM.
.
 
display-result.
PERFORM perform varyingVARYING tally fromFROM 1 byBY 1 untilUNTIL tally = t2
moveMOVE int-field ofOF table-two(tally) toTO show
DISPLAY FUNCTION display trimTRIM(show) ", " withWITH noNO advancingADVANCING
endEND-performPERFORM
moveMOVE int-field ofOF table-two(tally) toTO show
DISPLAY FUNCTION display trimTRIM(show).
.
 
END end programPROGRAM array-concat.</syntaxhighlight>
{{out}}
<pre>prompt$ cobc -xjd array-concatenation.cob --std=cobol2014 # COBOL 2014 needed for FUNCTION TRIM
6, 12, 18, 3, 6, 9, 12
</pre>
Line 1,669 ⟶ 1,766:
<syntaxhighlight lang="lisp">(vconcat '[1 2 3] '[4 5] '[6 7 8 9])
=> [1 2 3 4 5 6 7 8 9]</syntaxhighlight>
 
=={{header|EMal}}==
<syntaxhighlight lang="emal">
^|EMal has the concept of list expansion,
|you can expand a list to function arguments
|by prefixing it with the unary plus.
|^
List a = int[1,2,3]
List b = int[4,5,6]
List c = int[+a, +b]
writeLine(c)
</syntaxhighlight>
{{out}}
<pre>
[1,2,3,4,5,6]
</pre>
 
=={{header|Erlang}}==
Line 1,815 ⟶ 1,928:
Since FPC (Free Pascal compiler) version 3.2.0., the dynamic array concatenation operator <code>+</code> is available, provided <code>{$modeSwitch arrayOperators+}</code> (which is enabled by default in <code>{$mode Delphi}</code>).
<syntaxhighlight lang="pascal"> array2 := array0 + array1</syntaxhighlight>
Alternatively, one could use <code>concat()</code> which is independent of above modeswitch and mode. Neither option requires the use of any libraries.:
<syntaxhighlight lang="pascal"> array2 := concat(array0, array1);</syntaxhighlight>
 
Both options do not require any libraries.
A more complete example:
<syntaxhighlight lang="pascal">
Program arrayConcat;
 
{$mode delphi}
 
type
TDynArr = array of integer;
 
var
i: integer;
arr1, arr2, arrSum : TDynArr;
 
begin
arr1 := [1, 2, 3];
arr2 := [4, 5, 6];
 
arrSum := arr1 + arr2;
for i in arrSum do
write(i, ' ');
writeln;
end.
</syntaxhighlight>
{{out}}
<pre>
1 2 3 4 5 6
</pre>
 
=={{header|FreeBASIC}}==
Line 2,253 ⟶ 2,394:
let B be {4, 5, 6};
add B to A;</syntaxhighlight>
 
=={{header|Insitux}}==
<syntaxhighlight lang="insitux">(into [1 2 3] [4 5 6])</syntaxhighlight>
 
<syntaxhighlight lang="insitux">(.. vec [1 2 3] [4 5 6])</syntaxhighlight>
 
=={{header|Ioke}}==
Line 2,306 ⟶ 2,452:
2 3 3</syntaxhighlight>
 
=={{header|JavaJakt}}==
<syntaxhighlight lang="java5jakt">public static Object[] concat(Object[] arr1, Object[] arr2) {
fn main() {
Object[] res = new Object[arr1.length + arr2.length];
let a = ["Apple", "Banana"]
let b = ["Cherry", "Durian"]
mut c: [String] = []
c.push_values(&a)
c.push_values(&b)
println("{}", c)
}
</syntaxhighlight>
 
{{out}}
System.arraycopy(arr1, 0, res, 0, arr1.length);
<pre>
System.arraycopy(arr2, 0, res, arr1.length, arr2.length);
["Apple", "Banana", "Cherry", "Durian"]
</pre>
 
=={{header|Java}}==
return res;
In Java, arrays are immutable, so you'll have to create a new array, and copy the contents of the two arrays into it.<br />
}</syntaxhighlight>
Luckily, Java offers the ''System.arraycopy'' method, which will save you the effort of creating the for-loops.<br />
<syntaxhighlight lang="java">
int[] concat(int[] arrayA, int[] arrayB) {
int[] array = new int[arrayA.length + arrayB.length];
System.arraycopy(arrayA, 0, array, 0, arrayA.length);
System.arraycopy(arrayB, 0, array, arrayA.length, arrayB.length);
return array;
}
</syntaxhighlight>
If you wanted to use for-loops, possibly to mutate the data as it's concatenated, you can use the following.
<syntaxhighlight lang="java">
int[] concat(int[] arrayA, int[] arrayB) {
int[] array = new int[arrayA.length + arrayB.length];
for (int index = 0; index < arrayA.length; index++)
array[index] = arrayA[index];
for (int index = 0; index < arrayB.length; index++)
array[index + arrayA.length] = arrayB[index];
return array;
}
</syntaxhighlight>
A less idiomatic approach would be to use a ''List'', which is a mutable array, similar to a "vector" in other languages.<br />
I have used both arrays and ''List''s extensively and have not noticed any sort of performance degradation, they appear to work equally as fast.<br />
It's worth noting that the Java Collections Framework, which contains the ''List'' class, is built specifically for Objects and not necessarily primitive data-types. Despite this, it's still worth using for primitives, although the conversion to and from arrays is somewhat abstruse.
<syntaxhighlight lang="java">
int[] concat(int[] arrayA, int[] arrayB) {
List<Integer> list = new ArrayList<>();
for (int value : arrayA) list.add(value);
for (int value : arrayB) list.add(value);
int[] array = new int[list.size()];
for (int index = 0; index < list.size(); index++)
array[index] = list.get(index);
return array;
}
</syntaxhighlight>
 
=={{header|JavaScript}}==
Line 2,491 ⟶ 2,681:
arr3 = array(4, 5, 6)
arr3 = array(1, 2, 3, 4, 5, 6)</syntaxhighlight>
 
=={{header|LDPL}}==
{{libheader|ldpl-std}}
<syntaxhighlight lang="ldpl">include "std-list.ldpl"
 
data:
arr1 is number list
arr2 is number list
 
procedure:
push 1 to arr1
push 2 to arr1
push 3 to arr2
push 4 to arr2
append list arr2 to list arr1
display list arr1
</syntaxhighlight>
{{out}}
<pre>
[1, 2, 3, 4]
</pre>
 
=={{header|LFE}}==
Line 2,501 ⟶ 2,712:
 
=={{header|Liberty BASIC}}==
{{works with|Just BASIC}}
{{works with|Run BASIC}}
<syntaxhighlight lang="lb"> x=10
y=20
Line 2,989 ⟶ 3,202:
c[0..5] = a
c[6..10] = b</syntaxhighlight>
 
=={{header|Nu}}==
<syntaxhighlight lang="nu">
let a = [1 2 3]
let b = [4 5 6]
[$a $b] | flatten
</syntaxhighlight>
{{out}}
<pre>
╭───┬───╮
│ 0 │ 1 │
│ 1 │ 2 │
│ 2 │ 3 │
│ 3 │ 4 │
│ 4 │ 5 │
│ 5 │ 6 │
╰───┴───╯
</pre>
 
=={{header|Oberon-2}}==
Line 3,111 ⟶ 3,342:
# let array1and2 = Array.append array1 array2;;
val array1and2 : int array = [|1; 2; 3; 4; 5; 6|]</syntaxhighlight>
 
=={{header|Odin}}==
<syntaxhighlight lang="odin">package main
 
import "core:fmt"
import "core:slice"
 
main :: proc() {
x: [3]int = {1, 2, 3}
y: [3]int = {4, 5, 6}
 
xy: [len(x) + len(y)]int
copy(xy[:], x[:])
copy(xy[len(x):], y[:])
 
fmt.println(xy)
}</syntaxhighlight>
===Using slices===
<syntaxhighlight lang="odin">package main
 
import "core:fmt"
import "core:slice"
 
main :: proc() {
x: [3]int = {1, 2, 3}
y: [3]int = {4, 5, 6}
 
xy := slice.concatenate([][]int{x[:], y[:]})
defer delete(xy)
 
fmt.println(xy)
}</syntaxhighlight>
 
=={{header|Oforth}}==
Line 4,086 ⟶ 4,349:
 
<syntaxhighlight lang="slope">(list-join [1 2 3] [4 5 6])</syntaxhighlight>
 
=={{header|SmallBASIC}}==
<syntaxhighlight lang="SmallBASIC">
A = [1,2,3]
B = [4,5,6]
 
for i in B do A << i
 
print A
</syntaxhighlight>
 
=={{header|Smalltalk}}==
Line 4,452 ⟶ 4,725:
 
=={{header|Wren}}==
<syntaxhighlight lang="ecmascriptwren">var arr1 = [1,2,3]
var arr2 = [4,5,6]
System.print(arr1 + arr2)</syntaxhighlight>
{{Out}}
<pre>[1, 2, 3, 4, 5, 6]</pre>
 
=={{header|XPL0}}==
{{trans|C}}
A way to concatenate two XPL0 arrays when you know their size (and usually it is so).
Works on Raspberry Pi. MAlloc works differently in other versions.
<syntaxhighlight lang "XPL0">func Array_concat(A, AN, B, BN, S);
int A, AN, B, BN, S;
int P;
[
P:= MAlloc(S * (AN + BN));
CopyMem(P, A, AN*S);
CopyMem(P + AN*S, B, BN*S);
return P;
];
 
\ testing
int A, B, C, I, SizeOf;
[
A:= [ 1, 2, 3, 4, 5 ];
B:= [ 6, 7, 8, 9, 0 ];
 
SizeOf:= @B - @A;
 
C:= Array_concat(A, 5, B, 5, SizeOf);
 
for I:= 0 to 10-1 do
[IntOut(0, C(I)); ChOut(0, ^ )];
 
Release(C);
]</syntaxhighlight>
{{out}}
<pre>
1 2 3 4 5 6 7 8 9 0 </pre>
 
=={{header|Yabasic}}==
Line 4,549 ⟶ 4,855:
 
=={{header|Zig}}==
There are no hidden memory allocations in Zig.
<syntaxhighlight lang="zig">const std = @import("std");
<syntaxhighlight lang="zig">
fn concat(allocator: std.mem.Allocator, a: []const u32, b: []const u32) ![]u32 {
const resultstd = try allocator.alloc@import(u32, a.len + b.len"std");
std.mem.copy(u32, result, a);
std.mem.copy(u32, result[a.len..], b);
return result;
}
 
pub fn main() !void {
var general_purpose_allocatorgpa = std.heap.GeneralPurposeAllocator(.{}){};
defer _ = gpa.deinit();
const gpa = general_purpose_allocator.allocator();
 
var array1 = [_]u32{ 1, 2, 3, 4, 5 };
const allocator = gpa.allocator();
var array2 = [_]u32{ 6, 7, 8, 9, 10, 11, 12 };
 
const array3 = concat(gpa, &array1, &array2);
var array1 = [_]u32{ 1, 2, 3, 4, 5 };
std.debug.print("Array 1: {any}\nArray 2: {any}\nArray 3: {any}", .{ array1, array2, array3 });
var array2 = [_]u32{ 6, 7, 8, 9, 10, 11, 12 };
}</syntaxhighlight>
 
const slice3 = try std.mem.concat(allocator, u32, &[_][]const u32{ &array1, &array2 });
defer allocator.free(slice3);
 
// Same result, alternative syntax
const slice4 = try std.mem.concat(allocator, u32, &[_][]const u32{ array1[0..], array2[0..] });
defer allocator.free(slice4);
 
std.debug.print(
"Array 1: {any}\nArray 2: {any}\nSlice 3: {any}\nSlice 4: {any}\n",
.{ array1, array2, slice3, slice4 },
);
}
</syntaxhighlight>
{{out}}
<pre>
Array 1: { 1, 2, 3, 4, 5 }
Array 2: { 6, 7, 8, 9, 10, 11, 12 }
Slice 3: { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 }
Slice 4: { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 }
</pre>
 
=={{header|zkl}}==
56

edits