Array concatenation: Difference between revisions

PascalABC.NET
(PascalABC.NET)
 
(75 intermediate revisions by 45 users not shown)
Line 1:
{{task|Data Structures}}
[[Category:Simple]]
{{task|Data Structures}}
;Task:
Line 10:
 
=={{header|11l}}==
<langsyntaxhighlight lang="11l">V arr1 = [1, 2, 3]
V arr2 = [4, 5, 6]
print(arr1 [+] arr2)</langsyntaxhighlight>
{{out}}
<pre>
[1, 2, 3, 4, 5, 6]
</pre>
 
=={{header|68000 Assembly}}==
In order for this to work, you'll either need to use <code>malloc()</code> or know a memory location of "free space" at compile time. This example shall use the latter.
 
<syntaxhighlight lang="68000devpac">ArrayRam equ $00FF2000 ;this label points to 4k of free space.
 
;concatenate Array1 + Array2
LEA ArrayRam,A0
LEA Array1,A1
MOVE.W #5-1,D1 ;LEN(Array1), measured in words.
JSR memcpy_w
;after this, A0 will point to the destination of the second array.
 
LEA Array2,A1 ;even though the source arrays are stored back-to-back in memory, we'll assume they're not just for demonstration purposes.
MOVE.W #5-1,D1 ;LEN(Array2), measured in words
JSR memcpy_w
 
JMP * ;halt the CPU
memcpy_w:
MOVE.W (A1)+,(A0)+
DBRA D1,memcpy_w
rts
 
Array1:
DC.W 1,2,3,4,5
Array2:
DC.W 6,7,8,9,10</syntaxhighlight>
 
=={{header|8th}}==
<syntaxhighlight lang="forth">
<lang Forth>
[1,2,3] [4,5,6] a:+ .
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 29 ⟶ 56:
=={{header|AArch64 Assembly}}==
{{works with|as|Raspberry Pi 3B version Buster 64 bits}}
<syntaxhighlight lang="aarch64 assembly">
<lang AArch64 Assembly>
/* ARM assembly AARCH64 Raspberry PI 3B */
/* program concAreaString.s */
Line 131 ⟶ 158:
/* for this file see task include a file in language AArch64 assembly */
.include "../includeARM64.inc"
</syntaxhighlight>
</lang>
 
=={{header|ABAP}}==
The concept of arrays does not exist in ABAP, instead internal tables are used. This works in ABAP version 7.40 and above.
 
<syntaxhighlight lang="abap">
<lang ABAP>
report z_array_concatenation.
 
Line 147 ⟶ 174:
write <line>.
endloop.
</syntaxhighlight>
</lang>
 
{{out}}
Line 157 ⟶ 184:
=={{header|ACL2}}==
This is for lists, not arrays; ACL2's array support is limited.
<syntaxhighlight lang Lisp="lisp">(append xs ys)</langsyntaxhighlight>
 
=={{header|Action!}}==
<syntaxhighlight lang="action!">BYTE FUNC Concat(INT ARRAY src1,src2,dst BYTE size1,size2)
BYTE i
 
FOR i=0 TO size1-1
DO
dst(i)=src1(i)
OD
FOR i=0 TO size2-1
DO
dst(size1+i)=src2(i)
OD
RETURN (size1+size2)
 
PROC PrintArray(INT ARRAY a BYTE size)
BYTE i
 
Put('[)
FOR i=0 TO size-1
DO
PrintI(a(i))
IF i<size-1 THEN
Put(' )
FI
OD
Put('])
RETURN
 
PROC Test(INT ARRAY src1,src2 BYTE size1,size2)
INT ARRAY res(20)
BYTE size
 
size=Concat(src1,src2,res,size1,size2)
PrintArray(src1,size1)
Put('+)
PrintArray(src2,size2)
Put('=)
PrintArray(res,size)
PutE() PutE()
RETURN
 
PROC Main()
INT ARRAY
a1=[1 2 3 4],
a2=[5 6 7 8 9 10],
;a workaround for a3=[-1 -2 -3 -4 -5]
a3=[65535 65534 65533 65532 65531]
 
Test(a1,a2,4,6)
Test(a2,a1,6,4)
Test(a3,a2,5,4)
RETURN</syntaxhighlight>
{{out}}
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Array_concatenation.png Screenshot from Atari 8-bit computer]
<pre>
[1 2 3 4]+[5 6 7 8 9 10]=[1 2 3 4 5 6 7 8 9 10]
 
[5 6 7 8 9 10]+[1 2 3 4]=[5 6 7 8 9 10 1 2 3 4]
 
[-1 -2 -3 -4 -5]+[5 6 7 8]=[-1 -2 -3 -4 -5 5 6 7 8]
</pre>
 
=={{header|ActionScript}}==
<langsyntaxhighlight ActionScriptlang="actionscript">var array1:Array = new Array(1, 2, 3);
var array2:Array = new Array(4, 5, 6);
var array3:Array = array1.concat(array2); //[1, 2, 3, 4, 5, 6]</langsyntaxhighlight>
 
=={{header|Ada}}==
In [[Ada]] arrays are concatenated using the operation &. It works with any one dimensioned array:
<langsyntaxhighlight Adalang="ada">type T is array (Positive range <>) of Integer;
X : T := (1, 2, 3);
Y : T := X & (4, 5, 6); -- Concatenate X and (4, 5, 6)</langsyntaxhighlight>
 
=={{header|Aime}}==
<langsyntaxhighlight lang="aime">ac(list a, b)
{
list o;
Line 193 ⟶ 282:
 
0;
}</langsyntaxhighlight>
{{Out}}
<pre> 1 2 3 4 5 6 7 8</pre>
Line 204 ⟶ 293:
<!-- {{not tested with|ELLA ALGOL 68|Any (with appropriate job cards) - tested with release [http://sourceforge.net/projects/algol68/files/algol68toc/algol68toc-1.8.8d/algol68toc-1.8-8d.fc9.i386.rpm/download 1.8.8d.fc9.i386]}} -->
Includes operators for ''appending'' and ''prefixing'' an array to an existing flexible array:
<langsyntaxhighlight Algol68lang="algol68">MODE ARGTYPE = INT;
MODE ARGLIST = FLEX[0]ARGTYPE;
 
Line 233 ⟶ 322:
 
VOID(a +=: b);
print(("a +=: b", b, new line))</langsyntaxhighlight>
<pre>
a + b +1 +2 +3 +4 +5
Line 242 ⟶ 331:
=={{header|ALGOL W}}==
Algol W does not allow procedures to return arrays and has no mechanism for procedures to find the bounds of their parameters, so the caller must supply an array to concatenate into and the bounds of the arrays.
<langsyntaxhighlight lang="algolw">begin
integer array a ( 1 :: 5 );
integer array b ( 2 :: 4 );
Line 277 ⟶ 366:
for cPos := 1 until 8 do writeon( i_w := 1, s_w := 1, c( cPos ) )
 
end.</langsyntaxhighlight>
{{out}}
<pre>
1 2 3 4 5 -2 -3 -4
</pre>
 
=={{header|Amazing Hopper}}==
<syntaxhighlight lang="amazing hopper">
#include <hbasic.h>
Begin
a1 = {}
a2 = {}
Take(100,"Hola",0.056,"Mundo!"), and Push All(a1)
Take("Segundo",0,"array",~True,~False), and Push All(a2)
Concat (a1, a2) and Print ( a2, Newl )
End
</syntaxhighlight>
{{out}}
<pre>
Segundo,0,array,1,0,100,Hola,0.056,Mundo!
</pre>
 
=={{header|AntLang}}==
<langsyntaxhighlight AntLanglang="antlang">a:<1; <2; 3>>
b: <"Hello"; 42>
c: a,b</langsyntaxhighlight>
 
=={{header|Apex}}==
<langsyntaxhighlight lang="apex">List<String> listA = new List<String> { 'apple' };
List<String> listB = new List<String> { 'banana' };
listA.addAll(listB);
System.debug(listA); // Prints (apple, banana)</langsyntaxhighlight>
 
=={{header|APL}}==
<langsyntaxhighlight lang="apl">
1 2 3 , 4 5 6
1 2 3 4 5 6
</syntaxhighlight>
</lang>
 
=={{header|AppleScript}}==
<syntaxhighlight lang="applescript">
<lang AppleScript>
set listA to {1, 2, 3}
set listB to {4, 5, 6}
return listA & listB
</syntaxhighlight>
</lang>
 
{{out}}
Line 317 ⟶ 422:
{{trans|JavaScript}}
 
<langsyntaxhighlight lang="applescript">on run
 
concat([["alpha", "beta", "gamma"], ¬
Line 335 ⟶ 440:
end concat
 
</syntaxhighlight>
</lang>
 
{{Out}}
Line 343 ⟶ 448:
=={{header|ARM Assembly}}==
{{works with|as|Raspberry Pi}}
<syntaxhighlight lang="arm assembly">
<lang ARM Assembly>
/* ARM assembly Raspberry PI */
/* program concAreaString.s */
Line 515 ⟶ 620:
.Ls_magic_number_10: .word 0x66666667
 
</syntaxhighlight>
</lang>
 
=={{header|Arturo}}==
<langsyntaxhighlight lang="rebol">arr1: [1 2 3]
arr2: ["four" "five" "six"]
print arr1 ++ arr2</langsyntaxhighlight>
{{out}}
 
<pre>1 2 3 four five six</pre>
 
=={{header|ATS}}==
 
The following may seem frightening. However, it probably compiles down to two calls to __builtin_memcpy. All the complexity is to make sure those calls are done ''correctly''.
 
<syntaxhighlight lang="ats">(* The Rosetta Code array concatenation task, in ATS2. *)
 
(* In a way, the task is misleading: in a language such as ATS, one
can always devise a very-easy-to-use array type, put the code for
that in a library, and overload operators. Thus we can have
"array1 + array2" as array concatenation in ATS, complete with
garbage collection when the result no longer is needed.
 
It depends on what libraries are in one's repertoire.
 
Nevertheless, it seems fair to demonstrate how to concatenate two
barebones arrays at the nitpicking lowest level, without anything
but the barest contents of the ATS2 prelude. It will make ATS
programming look difficult; but ATS programming *is* difficult,
when you are using it to overcome the programming safety
deficiencies of a language such as C, without losing the runtime
efficiency of C code.
 
What we want is the kind of routine that would be used *in the
implementation* of "array1 + array2". So let us begin ... *)
 
#include "share/atspre_staload.hats" (* Loads some needed template
code. *)
 
fn {t : t@ype}
 
(* The demonstration will be for arrays of a non-linear type t. Were
the arrays to contain a *linear* type (vt@ype), then either the old
arrays would have to be destroyed or a copy procedure would be
needed for the elements. *)
 
arrayconcat1 {m, n : nat}
{pa, pb, pc : addr}
(pfa : !(@[t][m]) @ pa,
pfb : !(@[t][n]) @ pb,
pfc : !(@[t?][m + n]) @ pc >> @[t][m + n] @ pc |
pa : ptr pa,
pb : ptr pb,
pc : ptr pc,
m : size_t m,
n : size_t n) : void =
 
(* The routine takes as arguments three low-level arrays, passed by
value, as pointers with associated views. The first array is of
length m, with elements of type t, and the array must have been
initialized; the second is a similar array of length n. The third
array is uninitialized (thus the "?" character) and must have
length m+n; its type will change to "initialized". *)
 
{
prval (pfleft, pfright) = array_v_split {t?} {pc} {m + n} {m} pfc
 
(* We have had to split the view of array c into a left part pfleft,
of length m, and a right part pfright of length n. Arrays a and b
will be copied into the respective parts of c. *)
 
val _ = array_copy<t> (!pc, !pa, m)
val _ = array_copy<t> (!(ptr_add<t> (pc, m)), !pb, n)
 
(* Copying an array *safely* is more complex than what we are doing
here, but above the task has been given to the "array_copy"
template in the prelude. The "!" signs appear because array_copy is
call-by-reference but we are passing it pointers. *)
 
(* pfleft and pfright now refer to *initialized* arrays: one of length
m, starting at address pc; the other of length n, starting at
address pc+(m*sizeof<t>). *)
 
prval _ = pfc := array_v_unsplit {t} {pc} {m, n} (pfleft, pfright)
 
(* Before we can exit, the view of array c has to be replaced. It is
replaced by "unsplitting" the (now initialized) left and right
parts of the array. *)
 
(* We are done. Everything should now work, and the result will be
safe from buffer overruns or underruns, and against accidental
misuse of uninitialized data. *)
 
}
 
(* arrayconcat2 is a pass-by-reference interface to arrayconcat1. *)
fn {t : t@ype}
arrayconcat2 {m, n : nat}
(a : &(@[t][m]),
b : &(@[t][n]),
c : &(@[t?][m + n]) >> @[t][m + n],
m : size_t m,
n : size_t n) : void =
arrayconcat1 (view@ a, view@ b, view@ c |
addr@ a, addr@ b, addr@ c, m, n)
 
(* Overloads to let you say "arrayconcat" for either routine above. *)
overload arrayconcat with arrayconcat1
overload arrayconcat with arrayconcat2
 
implement
main0 () =
 
(* A demonstration program. *)
 
let
(* Some arrays on the stack. Because they are on the stack, they
will not need explicit freeing. *)
var a = @[int][3] (1, 2, 3)
var b = @[int][4] (5, 6, 7, 8)
var c : @[int?][7]
 
in
 
(* Compute c as the concatenation of a and b. *)
arrayconcat<int> (a, b, c, i2sz 3, i2sz 4);
 
(* The following simply prints the result. *)
let
(* Copy c to a linear linked list, because the prelude provides
means to easily print such a list. *)
val lst = array2list (c, i2sz 7)
in
println! (lst); (* Print the list. *)
free lst (* The list is linear and must be freed. *)
end
end</syntaxhighlight>
 
{{out}}
<pre>$ patscc -O2 -DATS_MEMALLOC_LIBC arrayconcat.dats && ./a.out
1, 2, 3, 5, 6, 7, 8</pre>
 
Footnotes:
 
* The ATS prelude does in fact translate calls to array_copy into C calls to memcpy. On a GNU system, the memcpy calls will likely become calls to __builtin_memcpy. The prelude's implementation is a practical one, rather than a strict demonstration of ATS methods.
 
* The "-DATS_MEMALLOC_LIBC" is needed due to the copying of an array to a linear linked list, which has to be both malloc'd and free'd. The arrays themselves are allocated on the stack, in this example.
 
=={{header|AutoHotkey}}==
=== True Arrays ===
{{works with|AutoHotkey_L}}
<langsyntaxhighlight AHKlang="ahk">List1 := [1, 2, 3]
List2 := [4, 5, 6]
cList := Arr_concatenate(List1, List2)
Line 547 ⟶ 789:
res .= ", " value
return "[" SubStr(res, 3) "]"
}</langsyntaxhighlight>
=== Legacy versions ===
[[AutoHotkey_Basic]] does not have real Arrays, but the user can implement them quite easily. For example:
<langsyntaxhighlight AutoHotkeylang="autohotkey">List1 = 1,2,3
List2 = 4,5,6
 
Line 586 ⟶ 828:
List .= (A_Index = 1 ? "" : ",") %Array%%A_Index%
Return, List
}</langsyntaxhighlight>
Message box shows:
<pre>1,2,3,4,5,6</pre>
Line 596 ⟶ 838:
 
<syntaxhighlight lang="autoit">
<lang AutoIt>
_ArrayConcatenate($avArray, $avArray2)
Func _ArrayConcatenate(ByRef $avArrayTarget, Const ByRef $avArraySource, $iStart = 0)
Line 615 ⟶ 857:
Return $iUBoundTarget + $iUBoundSource
EndFunc ;==>_ArrayConcatenate
</syntaxhighlight>
</lang>
 
=={{header|Avail}}==
<langsyntaxhighlight Availlang="avail"><1, 2, 3> ++ <¢a, ¢b, ¢c></langsyntaxhighlight>
 
=={{header|AWK}}==
<langsyntaxhighlight AWKlang="awk">#!/usr/bin/awk -f
BEGIN {
split("cul-de-sac",a,"-")
Line 639 ⟶ 881:
c[++nc]=b[i]
}
}</langsyntaxhighlight>
 
=={{header|Babel}}==
<langsyntaxhighlight lang="babel">[1 2 3] [4 5 6] cat ;</langsyntaxhighlight>
 
{{Out}}
Line 648 ⟶ 890:
 
=={{header|bash}}==
<langsyntaxhighlight lang="bash">x=("1 2" "3 4")
y=(5 6)
sum=( "${x[@]}" "${y[@]}" )
Line 656 ⟶ 898:
3 4
5
6</langsyntaxhighlight>
 
=={{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)
30 FOR I = 1 TO X:A(I - 1) = I: NEXT
40 FOR I = 1 TO Y:B(I - 1) = I * 10: NEXT
50 FOR I = 1 TO X:C(I - 1) = A(I - 1): NEXT
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}}===
<langsyntaxhighlight lang="bacon">DECLARE a[] = { 1, 2, 3, 4, 5 }
DECLARE b[] = { 6, 7, 8, 9, 10 }
 
Line 669 ⟶ 1,014:
c[x] = a[x]
c[x+5] = b[x]
NEXT</langsyntaxhighlight>
 
==={{header|BBC BASIC}}===
{{works with|BBC BASIC for Windows}}
<langsyntaxhighlight lang="bbcbasic"> DIM a(3), b(4)
a() = 1, 2, 3, 4
b() = 5, 6, 7, 8, 9
Line 691 ⟶ 1,036:
SYS "RtlMoveMemory", ^c(0), ^a(0), s%*na%
SYS "RtlMoveMemory", ^c(na%), ^b(0), s%*nb%
ENDPROC</langsyntaxhighlight>
 
==={{header|Commodore BASIC}}===
(Based on ZX Spectrum BASIC version)
<langsyntaxhighlight lang="basic">10 X=4 : Y=5
20 DIM A(X) : DIM B(Y) : DIM C(X+Y)
30 FOR I=1 TO X
Line 711 ⟶ 1,056:
150 FOR I=1 TO X+Y
160 : PRINT C(I);
170 NEXT</langsyntaxhighlight>
 
==={{header|Run BASIC}}===
{{works with|Just BASIC}}
{{works with|Liberty BASIC}}
The [[#Liberty BASIC|Liberty BASIC]] solution works without any changes.
 
=={{header|BASIC256}}==
<syntaxhighlight lang="basic256">arraybase 1
global c
 
dimen = 5
dim a(dimen)
dim b(dimen)
# Array initialization
for i = 1 to dimen
a[i] = i
b[i] = i + dimen
next i
 
nt = ConcatArrays(a, b)
 
for i = 1 to nt
print c[i];
if i < nt then print ", ";
next i
end
 
function ConcatArrays(a, b)
ta = a[?]
tb = b[?]
 
nt = ta + tb
redim c(nt)
 
for i = 1 to ta
c[i] = a[i]
next i
for i = 1 to tb
c[i + ta] = b[i]
next i
 
return nt
end function</syntaxhighlight>
{{out}}
<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}}==
<syntaxhighlight lang ="bqn">1‿2‿3 ∾ 4‿5‿6</langsyntaxhighlight>
 
=={{header|Bracmat}}==
Line 738 ⟶ 1,134:
=={{header|Burlesque}}==
 
<langsyntaxhighlight lang="burlesque">
blsq ) {1 2 3}{4 5 6}_+
{1 2 3 4 5 6}
</syntaxhighlight>
</lang>
 
=={{header|C}}==
A way to concatenate two C arrays when you know their size (and usually so it is)
<langsyntaxhighlight lang="c">#include <stdlib.h>
#include <stdio.h>
#include <string.h>
Line 776 ⟶ 1,172:
free(c);
return EXIT_SUCCCESS;
}</langsyntaxhighlight>
 
=={{header|C sharp|C#}}==
<langsyntaxhighlight lang="csharp">using System;
 
namespace RosettaCode
Line 800 ⟶ 1,196:
}
}
}</langsyntaxhighlight>
 
Alternatively, using LINQ extension methods:
 
{{works with|C sharp|C#|3}}
<langsyntaxhighlight lang="csharp">using System.Linq;
 
class Program
Line 816 ⟶ 1,212:
int[] c = a.Concat(b).ToArray();
}
}</langsyntaxhighlight>
 
=={{header|C++}}==
<langsyntaxhighlight lang="cpp">#include <vector>
#include <iostream>
 
Line 832 ⟶ 1,228:
for (int i = 0; i < a.size(); ++i)
std::cout << "a[" << i << "] = " << a[i] << "\n";
}</langsyntaxhighlight>
 
{{works with|C++11}}
Similar to above but using initialization schematics.
 
<langsyntaxhighlight lang="cpp">#include <vector>
#include <iostream>
 
Line 849 ⟶ 1,245:
std::cout << std::endl;
return 0;
}</langsyntaxhighlight>
 
This is another solution with function level templates and pointers.
 
<langsyntaxhighlight lang="cpp">#include <iostream>
 
using namespace std;
Line 902 ⟶ 1,298:
 
return 0;
}</langsyntaxhighlight>
 
=={{header|Ceylon}}==
<langsyntaxhighlight lang="ceylon">shared void arrayConcatenation() {
value a = Array {1, 2, 3};
value b = Array {4, 5, 6};
value c = concatenate(a, b);
print(c);
}</langsyntaxhighlight>
 
=={{header|Clojure}}==
<langsyntaxhighlight lang="clojure">(concat [1 2 3] [4 5 6])</langsyntaxhighlight>
The inputs can be any collection, including Java arrays, and returns a lazy sequence of the elements.
 
A vector is the closest Clojure thing to an array. If a vector is wanted, then use
<langsyntaxhighlight lang="clojure">(into [1 2 3] [4 5 6])</langsyntaxhighlight>
 
=={{header|COBOL}}==
{{works with|COBOL 2014}}
<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.
PERFORM 01 tableinitialize-one.tables
PERFORM concatenate-tables
05 int-field pic 999 occurs 0 to 5 depending on t1.
PERFORM 01 tabledisplay-two.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.</lang>
{{out}}
<pre>prompt$ cobc -xjd array-concatenation.cob --std=cobol2014 # COBOL 2014 needed for FUNCTION TRIM
6, 12, 18, 3, 6, 9, 12
</pre>
 
=={{header|CoffeeScript}}==
<langsyntaxhighlight lang="coffeescript">
# like in JavaScript
a = [1, 2, 3]
b = [4, 5, 6]
c = a.concat b
</syntaxhighlight>
</lang>
 
=={{header|Common Lisp}}==
<code>[http://www.lispworks.com/documentation/HyperSpec/Body/f_concat.htm concatenate]</code> is a general function for concatenating any type of sequence. It takes the type of sequence to produce, followed by any number of sequences of any type.
<langsyntaxhighlight lang="lisp">(concatenate 'vector #(0 1 2 3) #(4 5 6 7))
=> #(0 1 2 3 4 5 6 7)</langsyntaxhighlight>
===Alternate solution===
I use [https://franz.com/downloads/clp/survey Allegro CL 10.1]
 
<langsyntaxhighlight lang="lisp">
(setf arr1 (make-array '(3) :initial-contents '(1 2 3)))
(setf arr2 (make-array '(3) :initial-contents '(4 5 6)))
Line 1,011 ⟶ 1,398:
(write arr5)
(terpri)
</syntaxhighlight>
</lang>
Output:
<pre>
Line 1,020 ⟶ 1,407:
=={{header|Component Pascal}}==
BlackBox Component Builder
<langsyntaxhighlight lang="oberon2">
MODULE ArrayConcat;
IMPORT StdLog;
Line 1,076 ⟶ 1,463:
 
END ArrayConcat.
</syntaxhighlight>
</lang>
Execute: ^Q ArrayConcat.Do <br/>
{{out}}
Line 1,087 ⟶ 1,474:
 
=={{header|Crystal}}==
<langsyntaxhighlight lang="ruby">arr1 = [1, 2, 3]
arr2 = ["foo", "bar", "baz"]
arr1 + arr2 #=> [1, 2, 3, "foo", "bar", "baz"]</langsyntaxhighlight>
 
=={{header|D}}==
<langsyntaxhighlight lang="d">import std.stdio: writeln;
void main() {
Line 1,099 ⟶ 1,486:
writeln(a, " ~ ", b, " = ", a ~ b);
}</langsyntaxhighlight>
{{out}}
<pre>[1, 2] ~ [4, 5, 6] = [1, 2, 4, 5, 6]</pre>
 
=={{header|Delphi}}==
2022/07/13
<lang delphi>type
<syntaxhighlight lang="delphi">
// This example works on stuff as old as Delphi 5 (maybe older)
// Modern Delphi / Object Pascal has both
// • generic types
// • the ability to concatenate arrays with the '+' operator
// So I could just say:
// myarray := [1] + [2, 3];
// But if you do not have access to the latest/greatest, then:
{$apptype console}
 
type
// Array types must be declared in order to return them from functions
// They can also be used with open array parameters.
TArrayOfString = array of string;
 
function Concat( a, b : array of string ): TArrayOfString; overload;
{
Every array type needs its own 'Concat' function:
function Concat( a, b : array of integer ): TArrayOfInteger; overload;
function Concat( a, b : array of double ): TArrayOfDouble; overload;
etc
Also, dynamic and open array types ALWAYS start at 0. No need to complicate indexing here.
}
var
n : Integer;
begin
SetLength( result, Length(a)+Length(b) );
for n := 0 to High(a) do result[ n] := a[n];
for n := 0 to High(b) do result[Length(a)+n] := b[n]
end;
 
// Example time!
function Join( a : array of string; sep : string = ' ' ): string;
var
n : integer;
begin
if Length(a) > 0 then result := a[0];
for n := 1 to High(a) do result := result + sep + a[n]
end;
 
var
names : TArrayOfString;
begin
// Here we use the open array parameter constructor as a convenience
names := Concat( ['Korra', 'Asami'], ['Bolin', 'Mako'] );
WriteLn( Join(names) );
 
// Also convenient: open array parameters are assignment-compatible with our array type!
names := Concat( names, ['Varrick', 'Zhu Li'] );
WriteLn( #13#10, Join(names, ', ') );
names := Concat( ['Tenzin'], names );
Writeln( #13#10, Join(names, #13#10 ) );
end.
</syntaxhighlight>
Output:
Korra Asami Bolin Mako
Korra, Asami, Bolin, Mako, Varrick, Zhu Li
Tenzin
Korra
Asami
Bolin
Mako
Varrick
Zhu Li
 
<br>
What follows is older content found on this page.<br>
It has running commentary about memory management that isn’t exactly correct.<br>
Delphi handles dynamic array memory very well.
<syntaxhighlight lang="delphi">type
TReturnArray = array of integer; //you need to define a type to be able to return it
 
Line 1,141 ⟶ 1,601:
Finalize(r1); //IMPORTANT!
ShowMessage(IntToStr(High(r1)));
end;</langsyntaxhighlight>
 
=={{header|Diego}}==
<syntaxhighlight lang="diego">set_namespace(rosettacode)_me();
 
add_ary(a)_values(1,2,3);
add_ary(b)_values(4,5,6);
me_msg()_ary[a]_concat[b]
me_msg()_ary[a]_concat()_ary[b]; // alternative
me_msg()_calc([a]+[b]); // alternative
 
reset_namespace[];</syntaxhighlight>
 
=={{header|Dyalect}}==
 
<langsyntaxhighlight lang="dyalect">var xs = [1,2,3]
var ys = [4,5,6]
var alls = Array.concatConcat(xs, ys)
print(alls)</langsyntaxhighlight>
 
{{out}}
Line 1,156 ⟶ 1,627:
=={{header|E}}==
 
<langsyntaxhighlight lang="e">? [1,2] + [3,4]
# value: [1, 2, 3, 4]</langsyntaxhighlight>
 
=={{header|EasyLang}}==
 
<syntaxhighlight lang="text">a[] = [ 1 2 3 ]
b[] = [ 4 5 6 ]
c[] = a[]
whilefor ih < lenin b[]
c[] &= b[i]h
i += 1
.
print c[]</langsyntaxhighlight>
 
=={{header|EchoLisp}}==
The native operators are '''append''' for lists, and '''vector-append''' for vectors (1-dim arrays).
<langsyntaxhighlight lang="scheme">
;;;; VECTORS
(vector-append (make-vector 6 42) (make-vector 4 666))
Line 1,187 ⟶ 1,657:
 
 
</syntaxhighlight>
</lang>
 
=={{header|ECL}}==
 
<syntaxhighlight lang="text">
A := [1, 2, 3, 4];
B := [5, 6, 7, 8];
 
C := A + B;</langsyntaxhighlight>
 
=={{header|Ecstasy}}==
It is as simple as <code><var>array1</var> + <var>array2</var></code>:
<syntaxhighlight lang="java">String[] fruits = ["apples", "oranges"];
String[] grains = ["wheat", "corn"];
String[] all = fruits + grains;</syntaxhighlight>
 
=={{header|Efene}}==
Line 1,201 ⟶ 1,677:
using the ++ operator and the lists.append function
 
<langsyntaxhighlight lang="efene">
@public
run = fn () {
Line 1,212 ⟶ 1,688:
io.format("~p~n", [C])
io.format("~p~n", [D])
}</langsyntaxhighlight>
 
=={{header|EGL}}==
{{works with|EDT}}
<syntaxhighlight lang="egl">
<lang EGL>
program ArrayConcatenation
function main()
Line 1,230 ⟶ 1,706:
end
end
</syntaxhighlight>
</lang>
 
=={{header|Ela}}==
<langsyntaxhighlight lang="ela">xs = [1,2,3]
ys = [4,5,6]
xs ++ ys</langsyntaxhighlight>
{{out}}<pre>[1,2,3,4,5,6]</pre>
 
=={{header|Elena}}==
ELENA 5.0 :
<langsyntaxhighlight lang="elena">import extensions;
 
public program()
Line 1,250 ⟶ 1,726:
"(",a.asEnumerable(),") + (",b.asEnumerable(),
") = (",(a + b).asEnumerable(),")").readChar();
}</langsyntaxhighlight>
{{out}}
<pre>
Line 1,257 ⟶ 1,733:
 
=={{header|Elixir}}==
<langsyntaxhighlight lang="elixir">iex(1)> [1, 2, 3] ++ [4, 5, 6]
[1, 2, 3, 4, 5, 6]
iex(2)> Enum.concat([[1, [2], 3], [4], [5, 6]])
[1, [2], 3, 4, 5, 6]
iex(3)> Enum.concat([1..3, [4,5,6], 7..9])
[1, 2, 3, 4, 5, 6, 7, 8, 9]</langsyntaxhighlight>
 
=={{header|Elm}}==
<langsyntaxhighlight lang="elm">import Element exposing (show, toHtml) -- elm-package install evancz/elm-graphics
import Html.App exposing (beginnerProgram)
import Array exposing (Array, append, initialize)
Line 1,283 ⟶ 1,759:
}
 
-- Array.fromList [0,1,2,3,4,5]</langsyntaxhighlight>
 
=={{header|Emacs Lisp}}==
The ''vconcat'' function returns a new array containing all the elements of it's arguments.
 
<langsyntaxhighlight lang="lisp">(vconcat '[1 2 3] '[4 5] '[6 7 8 9])
=> [1 2 3 4 5 6 7 8 9]</langsyntaxhighlight>
 
=={{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,296 ⟶ 1,788:
 
On the shell,
<langsyntaxhighlight lang="erlang">
1> [1, 2, 3] ++ [4, 5, 6].
[1,2,3,4,5,6]
Line 1,302 ⟶ 1,794:
[1,2,3,4,5,6]
3>
</syntaxhighlight>
</lang>
 
=={{header|ERRE}}==
<syntaxhighlight lang="text">
PROGRAM ARRAY_CONCAT
 
Line 1,337 ⟶ 1,829:
 
END PROGRAM
</syntaxhighlight>
</lang>
 
=={{header|Euphoria}}==
<langsyntaxhighlight Euphorialang="euphoria">sequence s1,s2,s3
s1 = {1,2,3}
s2 = {4,5,6}
s3 = s1 & s2
? s3</langsyntaxhighlight>
 
{{out}}
Line 1,351 ⟶ 1,843:
=={{header|F Sharp|F#}}==
Array concatenation.
<langsyntaxhighlight lang="fsharp">let a = [|1; 2; 3|]
let b = [|4; 5; 6;|]
let c = Array.append a b</langsyntaxhighlight>
List concatenation (@ and List.append are equivalent).
<langsyntaxhighlight lang="fsharp">let x = [1; 2; 3]
let y = [4; 5; 6]
let z1 = x @ y
let z2 = List.append x y</langsyntaxhighlight>
 
=={{header|Factor}}==
<syntaxhighlight lang ="factor">append</langsyntaxhighlight>
 
'''Example''':
<langsyntaxhighlight lang="factor">( scratchpad ) USE: sequences
( scratchpad ) { 1 2 } { 3 4 } append .
{ 1 2 3 4 }</langsyntaxhighlight>
 
=={{header|Fantom}}==
Line 1,372 ⟶ 1,864:
In fansh:
 
<langsyntaxhighlight lang="fantom">
> a := [1,2,3]
> b := [4,5,6]
Line 1,378 ⟶ 1,870:
> a
[1,2,3,4,5,6]
</syntaxhighlight>
</lang>
 
Note 'addAll' is destructive. Write 'a.dup.addAll(b)' to create a fresh list.
Line 1,384 ⟶ 1,876:
=={{header|FBSL}}==
Array concatenation:
<langsyntaxhighlight lang="qbasic">#APPTYPE CONSOLE
 
DIM aint[] ={1, 2, 3}, astr[] ={"one", "two", "three"}, asng[] ={!1, !2, !3}
Line 1,392 ⟶ 1,884:
NEXT
 
PAUSE</langsyntaxhighlight>
{{out}}
<pre>1 2 3 one two three 1.000000 2.000000 3.000000
Line 1,398 ⟶ 1,890:
 
=={{header|Forth}}==
<langsyntaxhighlight Forthlang="forth">: $!+ ( a u a' -- a'+u )
2dup + >r swap move r> ;
: cat ( a2 u2 a1 u1 -- a3 u1+u2 )
Line 1,411 ⟶ 1,903:
801842600: 03 00 00 00 00 00 00 00 - 04 00 00 00 00 00 00 00 ................
801842610: 05 00 00 00 00 00 00 00 - ........
</syntaxhighlight>
</lang>
 
=={{header|Fortran}}==
{{works with|Fortran|90 and later}}
<langsyntaxhighlight lang="fortran">program Concat_Arrays
implicit none
 
Line 1,431 ⟶ 1,923:
d = [a, b] ! (/a, b/)
print*, d
end program Concat_Arrays</langsyntaxhighlight>
 
=={{header|Free Pascal}}==
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>).
<langsyntaxhighlight lang="pascal"> array2 := array0 + array1</langsyntaxhighlight>
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>
 
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}}==
<langsyntaxhighlight lang="freebasic">
' FB 1.05.0 Win64
 
Line 1,465 ⟶ 1,988:
Print "Press any key to quit the program"
Sleep
</syntaxhighlight>
</lang>
 
{{out}}
Line 1,473 ⟶ 1,996:
 
=={{header|Frink}}==
<langsyntaxhighlight lang="frink">
a = [1,2]
b = [3,4]
a.pushAll[b]
</syntaxhighlight>
</lang>
 
=={{header|FunL}}==
<langsyntaxhighlight lang="funl">arr1 = array( [1, 2, 3] )
arr2 = array( [4, 5, 6] )
arr3 = array( [7, 8, 9] )
 
println( arr1 + arr2 + arr3 )</langsyntaxhighlight>
 
{{out}}
Line 1,496 ⟶ 2,019:
Array concatenation is done with the built-in function <code>concat</code>, which can take any number of arguments:
 
<syntaxhighlight lang="futhark">
<lang Futhark>
concat as bs cd
</syntaxhighlight>
</lang>
 
=={{header|FutureBasic}}==
 
<syntaxhighlight lang="futurebasic">void local fn DoIt
CFArrayRef array = @[@"Alpha",@"Bravo",@"Charlie"]
print array
 
array = fn ArrayByAddingObjectsFromArray( array, @[@"Delta",@"Echo",@"FutureBasic"] )
print array
end fn
 
window 1
 
fn DoIt
 
HandleEvents</syntaxhighlight>
Output:
<pre>
(
Alpha,
Bravo,
Charlie
)
(
Alpha,
Bravo,
Charlie,
Delta,
Echo,
FutureBasic
)
</pre>
 
=={{header|Gambas}}==
'''[https://gambas-playground.proko.eu/?gist=314bea6fba7f177a1cfaec8a7a8b5ccb Click this link to run this code]'''
<langsyntaxhighlight lang="gambas">Public Sub Main()
Dim sString1 As String[] = ["The", "quick", "brown", "fox"]
Dim sString2 As String[] = ["jumped", "over", "the", "lazy", "dog"]
Line 1,510 ⟶ 2,065:
Print sString1.Join(" ")
 
End</langsyntaxhighlight>
Output:
<pre>
Line 1,517 ⟶ 2,072:
 
=={{header|GAP}}==
<langsyntaxhighlight lang="gap"># Concatenate arrays
Concatenation([1, 2, 3], [4, 5, 6], [7, 8, 9]);
# [ 1, 2, 3, 4, 5, 6, 7, 8, 9 ]
Line 1,526 ⟶ 2,081:
Append(a, [7, 8, 9]);
a;
# [ 1, 2, 3, 4, 5, 6, 7, 8, 9 ]</langsyntaxhighlight>
 
=={{header|Genie}}==
<langsyntaxhighlight lang="genie">[indent=4]
/*
Array concatenation, in Genie
Line 1,556 ⟶ 2,111:
stdout.printf("y: "); int_show_array(y)
stdout.printf("z: "); int_show_array(z)
print "%d elements in new array", z.length</langsyntaxhighlight>
 
{{out}}
Line 1,568 ⟶ 2,123:
=={{header|GLSL}}==
This macro concatenates two arrays to form a new array. The first parameter is the type of the array:
<langsyntaxhighlight lang="glsl">
#define array_concat(T,a1,a2,returned) \
T[a1.length()+a2.length()] returned; \
Line 1,579 ⟶ 2,134:
} \
}
</syntaxhighlight>
</lang>
The macro can be used like this:
<langsyntaxhighlight lang="glsl">
array_concat(float,float[](1.,2.,3.),float[](4.,5.,6.),returned);
int i = returned.length();
</syntaxhighlight>
</lang>
 
=={{header|Go}}==
<langsyntaxhighlight lang="go">package main
 
import "fmt"
Line 1,621 ⟶ 2,176:
fmt.Println(n)
 
}</langsyntaxhighlight>
{{out}}
<pre>
Line 1,629 ⟶ 2,184:
</pre>
Array concatenation needs can vary. Here is another set of examples that illustrate different techniques.
<langsyntaxhighlight lang="go">package main
 
import (
Line 1,685 ⟶ 2,240:
test2_c := ArrayConcat(test2_a, test2_b).([]string)
fmt.Println(test2_a, " + ", test2_b, " = ", test2_c)
}</langsyntaxhighlight>
{{out}}
<pre>
Line 1,694 ⟶ 2,249:
=={{header|Gosu}}==
 
<langsyntaxhighlight lang="gosu">
var listA = { 1, 2, 3 }
var listB = { 4, 5, 6 }
Line 1,701 ⟶ 2,256:
 
print( listC ) // prints [1, 2, 3, 4, 5, 6]
</syntaxhighlight>
</lang>
 
=={{header|Groovy}}==
Solution:
<langsyntaxhighlight lang="groovy">def list = [1, 2, 3] + ["Crosby", "Stills", "Nash", "Young"]</langsyntaxhighlight>
 
Test:
<syntaxhighlight lang ="groovy">println list</langsyntaxhighlight>
 
{{out}}
Line 1,715 ⟶ 2,270:
=={{header|Haskell}}==
A list is in Haskell one of the most common composite data types (constructed from other types). In the documentation we read for the append operation ++:
<langsyntaxhighlight lang="haskell">(++) :: [a] -> [a] -> [a]</langsyntaxhighlight>
Append two lists, i.e.:<pre>
[x1, ..., xm] ++ [y1, ..., yn] == [x1, ..., xm, y1, ..., yn]
Line 1,722 ⟶ 2,277:
 
This operator could be defined from the scratch using explicit recursion:
<langsyntaxhighlight lang="haskell">
[] ++ x = x
(h:t) ++ y = h : (t ++ y)
</syntaxhighlight>
</lang>
or folding
<langsyntaxhighlight lang="haskell">
x ++ y = foldr (:) y x
</syntaxhighlight>
</lang>
 
=={{header|HicEst}}==
<langsyntaxhighlight HicEstlang="hicest">REAL :: a(7), b(3), c(10)
 
c = a
DO i = 1, LEN(b)
c(i + LEN(a)) = b(i)
ENDDO</langsyntaxhighlight>
 
=={{header|Hy}}==
<langsyntaxhighlight lang="hy">=> (setv a [1 2 3])
=> a
[1, 2, 3]
Line 1,754 ⟶ 2,309:
 
=> (+ [1 2] [3 4] [5 6]) ; can accept multiple arguments
[1, 2, 3, 4, 5, 6]</langsyntaxhighlight>
 
=={{header|i}}==
<langsyntaxhighlight lang="i">main
a $= [1, 2, 3]
b $= [4, 5, 6]
print(a + b)
}</langsyntaxhighlight>
 
=={{header|Icon}} and {{header|Unicon}}==
Both languages have list concatenation built in. Lists are fully dynamic arrays which can be truncated or extended at either end.
<langsyntaxhighlight lang="icon">
procedure main()
L1 := [1, 2, 3, 4]
Line 1,777 ⟶ 2,332:
write()
end
</syntaxhighlight>
</lang>
 
=={{header|IDL}}==
 
Array concatenation can mean different things, depending on the number of dimensions of the arguments and the result. In the simplest case, with 1-dimensional arrays to begin with, there are two obvious ways to concatenate them. If my arrays are these:
<syntaxhighlight lang="idl">
<lang IDL>
> a = [1,2,3]
> b = [4,5,6]
Line 1,793 ⟶ 2,348:
> print,b
4 5 6
</syntaxhighlight>
</lang>
Then they can be concatenated "at the ends":
<syntaxhighlight lang="idl">
<lang IDL>
> help,[a,b]
<Expression> INT = Array[6]
> print,[a,b]
1 2 3 4 5 6
</syntaxhighlight>
</lang>
or "at the sides":
<syntaxhighlight lang="idl">
<lang IDL>
> help,[[a],[b]]
<Expression> INT = Array[3, 2]
Line 1,808 ⟶ 2,363:
1 2 3
4 5 6
</syntaxhighlight>
</lang>
Note that this requires that the arrays have the same size at the side at which they are concatenated:
<syntaxhighlight lang="idl">
<lang IDL>
> b = transpose(b)
> help,b
Line 1,824 ⟶ 2,379:
Unable to concatenate variables because the dimensions do not agree: B.
Execution halted at: $MAIN$
</syntaxhighlight>
</lang>
This can get a lot more complicated as a 3x4x5-element three-dimensional array can be concatenated with a 5x2x3-element array at exactly two "surfaces".
 
Line 1,836 ⟶ 2,391:
 
=={{header|Inform 7}}==
<langsyntaxhighlight lang="inform7">let A be {1, 2, 3};
let B be {4, 5, 6};
add B to A;</langsyntaxhighlight>
 
=={{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}}==
<langsyntaxhighlight lang="ioke">iik> [1,2,3] + [3,2,1]
[1,2,3] + [3,2,1]
+> [1, 2, 3, 3, 2, 1]</langsyntaxhighlight>
 
=={{header|J}}==
Line 1,849 ⟶ 2,409:
 
'''Example''':
<langsyntaxhighlight lang="j"> array1 =: 1 2 3
array2 =: 4 5 6
array1 , array2
1 2 3 4 5 6</langsyntaxhighlight>
 
Of course, in J, array concatenation works (consistently) on arrays of any rank or dimension.
Line 1,858 ⟶ 2,418:
The verb <code>,</code> concatenates by treating the argument array with the largest number of dimensions as a list. Other primary verbs concatenate along other axes.
 
<langsyntaxhighlight lang="j"> ]ab=: 3 3 $ 'aaabbbccc'
aaa
bbb
Line 1,890 ⟶ 2,450:
3 6
$ ab ,: wx NB. applies to new (higher) axis
2 3 3</langsyntaxhighlight>
 
=={{header|JavaJakt}}==
<syntaxhighlight lang="jakt">
<lang java5>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 />
}</lang>
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}}==
The <code>Array.concat()</code> method returns a new array comprised of this array joined with other array(s) and/or value(s).
<langsyntaxhighlight lang="javascript">var a = [1,2,3],
b = [4,5,6],
c = a.concat(b); //=> [1,2,3,4,5,6]</langsyntaxhighlight>
 
 
Line 1,914 ⟶ 2,518:
See, for a function with an analogous type signature, '''concat''' in the Haskell Prelude.
 
<langsyntaxhighlight lang="javascript">(function () {
'use strict';
 
Line 1,929 ⟶ 2,533:
);
 
})();</langsyntaxhighlight>
 
{{Out}}
 
<pre>["alpha", "beta", "gamma", "delta", "epsilon", "zeta", "eta", "theta", "iota"]</pre>
 
=={{header|Joy}}==
<syntaxhighlight lang="joy">[1 2 3] [4 5 6] concat.</syntaxhighlight>
 
=={{header|jq}}==
Line 1,941 ⟶ 2,548:
To concatenate the component arrays of an array, A, the <tt>add</tt> filter can be used: <tt>A|add</tt>
 
jq also supports streams, which are somewhat array-like, so it may be worth mentioning that the concatenation of two or more streams can be accomplished using "," instead of "+". <langsyntaxhighlight lang="jq">[1,2] + [3] + [null] # => [1,2,3,null]
 
[range(1;3), 3, null] # => [1,2,3,null]
</syntaxhighlight>
</lang>
 
=={{header|Julia}}==
<langsyntaxhighlight lang="julia">a = [1,2,3]
b = [4,5,6]
ab = [a;b]
Line 1,955 ⟶ 2,562:
ab = hcat(a,b) #ab -> 3x2 matrix
# the append!(a,b) method is mutating, appending `b` to `a`
append!(a,b) # a now equals [1,2,3,4,5,6]</langsyntaxhighlight>
 
=={{header|K}}==
<syntaxhighlight lang="k">
<lang K>
a: 1 2 3
b: 4 5 6
a,b
1 2 3 4 5 6</langsyntaxhighlight>
 
Concatenations on larger dimensions also use ",", often combined with other operations.
 
<syntaxhighlight lang="k">
<lang K>
ab:3 3#"abcdefghi"
("abc"
Line 1,998 ⟶ 2,605:
("abc036"
"def147"
"ghi258")</langsyntaxhighlight>
 
=={{header|Klingphix}}==
<langsyntaxhighlight Klingphixlang="klingphix">include ..\Utilitys.tlhy
 
( 1.0 "Hello" 3 2 / 4 2.1 power ) ( 5 6 7 8 ) chain print
 
" " input</langsyntaxhighlight>
{{out}}
<pre>(1, "Hello", 1.5, 18.379173679952562, 5, 6, 7, 8)</pre>
 
=={{header|Klong}}==
<syntaxhighlight lang="k">
<lang K>
[1 2 3],[4 5 6] :" join "
[1 2 3 4 5 6]
Line 2,019 ⟶ 2,626:
[1 2],:/[[3 4] [5 6] [7 8]] :" join each-right "
[[3 4 1 2] [5 6 1 2] [7 8 1 2]]
</syntaxhighlight>
</lang>
 
=={{header|Kotlin}}==
<syntaxhighlight lang="kotlin">fun main() {
There is no operator or standard library function for concatenating <code>Array</code> types. One option is to convert to <code>Collection</code>s, concatenate, and convert back:
val a = intArrayOf(1, 2, 3)
<lang kotlin>fun main(args: Array<String>) {
val a: Array<Int>b = arrayOfintArrayOf(14, 25, 36) // initialise a
val c = a + b: Array<Int>// =[1, 2, 3, arrayOf(4, 5, 6) // initialise b]
println(c.contentToString())
val c: Array<Int> = (a.toList() + b.toList()).toTypedArray()
}</syntaxhighlight>
println(c)
}</lang>
 
Alternatively, we can write our own concatenation function:
<lang kotlin>fun arrayConcat(a: Array<Any>, b: Array<Any>): Array<Any> {
return Array(a.size + b.size, { if (it in a.indices) a[it] else b[it - a.size] })
}</lang>
 
When working directly with <code>Collection</code>s, we can simply use the <code>+</code> operator:
<lang kotlin>fun main(args: Array<String>) {
val a: Collection<Int> = listOf(1, 2, 3) // initialise a
val b: Collection<Int> = listOf(4, 5, 6) // initialise b
val c: Collection<Int> = a + b
println(c)
}</lang>
 
=={{header|LabVIEW}}==
Line 2,048 ⟶ 2,641:
 
=={{header|Lambdatalk}}==
<langsyntaxhighlight lang="scheme">
{def A {A.new 1 2 3 4 5 6}} -> [1,2,3,4,5,6]
{def B {A.new 7 8 9}} -> [7,8,9]
{A.concat {A} {B}} -> [1,2,3,4,5,6,7,8,9]
</syntaxhighlight>
</lang>
 
=={{header|Lang}}==
<syntaxhighlight lang="lang">
&a $= [1, 2, 3]
&b $= [4, 5, 6]
&c $= &a ||| &b
fn.println(&c)
</syntaxhighlight>
 
=={{header|Lang5}}==
<langsyntaxhighlight Lang5lang="lang5">[1 2] [3 4] append collapse .</langsyntaxhighlight>
 
=={{header|langur}}==
<langsyntaxhighlight lang="langur">val .a = [1, 2, 3]
val .b = [7, 8, 9]
val .c = .a ~ .b
writeln .c</langsyntaxhighlight>
 
{{out}}
Line 2,067 ⟶ 2,668:
 
=={{header|Lasso}}==
<syntaxhighlight lang="lasso">
<lang Lasso>
local(arr1 = array(1, 2, 3))
local(arr2 = array(4, 5, 6))
Line 2,079 ⟶ 2,680:
arr2 = array(4, 5, 6)
arr3 = array(4, 5, 6)
arr3 = array(1, 2, 3, 4, 5, 6)</langsyntaxhighlight>
 
=={{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}}==
<langsyntaxhighlight lang="lisp">
> (++ '(1 2 3) '(4 5 6))
(1 2 3 4 5 6)
> (: lists append '(1 2 3) '(4 5 6))
(1 2 3 4 5 6)
</syntaxhighlight>
</lang>
 
=={{header|Liberty BASIC}}==
{{works with|Just BASIC}}
<lang lb> x=10
{{works with|Run BASIC}}
<syntaxhighlight lang="lb"> x=10
y=20
dim array1(x)
Line 2,107 ⟶ 2,731:
for i = 1 to x + y
print array3(i)
next</langsyntaxhighlight>
 
=={{header|LIL}}==
LIL uses lists instead of arrays. The builtin '''append''' command could be used as '''append a $b'''. That would add the entire list in variable '''b''' as one item to list '''a'''. Below '''quote''' is used to flatten the lists into a single new list of all items.
 
<langsyntaxhighlight lang="tcl">##
Array concatenation in LIL
##
Line 2,120 ⟶ 2,744:
 
print $c
print "[index $c 0] [index $c 3]"</langsyntaxhighlight>
 
{{out}}
Line 2,128 ⟶ 2,752:
 
=={{header|Limbo}}==
<langsyntaxhighlight lang="limbo">implement Command;
 
include "sys.m";
Line 2,150 ⟶ 2,774:
for (i := 0; i < len c; i++)
sys->print("%d\n", c[i]);
}</langsyntaxhighlight>
 
=={{header|Lingo}}==
<langsyntaxhighlight lang="lingo">a = [1,2]
b = [3,4,5]
 
Line 2,161 ⟶ 2,785:
 
put a
-- [1, 2, 3, 4, 5]</langsyntaxhighlight>
 
=={{header|Little}}==
<langsyntaxhighlight Clang="c">void main() {
int a[] = {0, 1, 2, 3, 4};
int b[] = {5, 6, 7, 8, 9};
int c[] = {(expand)a, (expand)b};
puts(c);
}</langsyntaxhighlight>
 
=={{header|Logo}}==
COMBINE is used to combine lists or words. SENTENCE is used to combine lists and words into a single list.
<langsyntaxhighlight lang="logo">
to combine-arrays :a1 :a2
output listtoarray sentence arraytolist :a1 arraytolist :a2
end
show combine-arrays {1 2 3} {4 5 6} ; {1 2 3 4 5 6}
</syntaxhighlight>
</lang>
 
=={{header|Lua}}==
<langsyntaxhighlight lang="lua">a = {1, 2, 3}
b = {4, 5, 6}
 
Line 2,188 ⟶ 2,812:
end
 
print(table.concat(a, ", "))</langsyntaxhighlight>
{{out}}
<pre>
Line 2,195 ⟶ 2,819:
 
=={{header|M2000 Interpreter}}==
<syntaxhighlight lang="m2000 interpreter">
<lang M2000 Interpreter>
a=(1,2,3,4,5)
b=Cons(a, (6,7,8),a)
Print b
1 2 3 4 5 6 7 8 1 2 3 4 5
</syntaxhighlight>
</lang>
 
Adding 2 dimension arrays
 
<syntaxhighlight lang="m2000 interpreter">
<lang M2000 Interpreter>
Dim Base 0, A(2,2)=1, B(1,2)=6
A()=Cons(A(), B(), A(), B())
Line 2,215 ⟶ 2,839:
Print
}
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 2,223 ⟶ 2,847:
1 1
1 1
6 6
</pre >
 
Adding 2 dimension arrays using OLE clause
 
<syntaxhighlight lang="m2000 interpreter">
Dim OLE Base 0, A(2,2)=1, B(1,2)=6
A()=Cons(A(), B(), A(), B())
\\ Restore the dimensions (without erasing items)
Dim A(Dimension(A(),1)/2, 2)
For I=0 to Dimension(A(),1)-1 {
For j=0 to Dimension(A(),2)-1 {
Print A(i, j),
}
Print
}
</syntaxhighlight>
{{out}}
<pre>
1 1
1 1
1 1
1 1
6 6
6 6
</pre >
Line 2,228 ⟶ 2,876:
=={{header|Maple}}==
There is a built-in procedure for concatenating arrays (and similar objects such as matrices or vectors). Arrays can be concatenated along any given dimension, which is specified as the first argument.
<syntaxhighlight lang="maple">
<lang Maple>
> A := Array( [ 1, 2, 3 ] );
A := [1, 2, 3]
Line 2,254 ⟶ 2,902:
[ ]
[1 2 3]
</syntaxhighlight>
</lang>
Of course, the order of the arguments is important.
<syntaxhighlight lang="maple">
<lang Maple>
> ArrayTools:-Concatenate( 1, A, M );
[1 2 3]
Line 2,263 ⟶ 2,911:
[ ]
[d e f]
</syntaxhighlight>
</lang>
Lists, in Maple, might be considered to be a kind of "array" (in the sense that they look like arrays in memory), though they are actually immutable objects. However, they can be concatenated as follows.
<syntaxhighlight lang="maple">
<lang Maple>
> L1 := [ 1, 2, 3 ];
L1 := [1, 2, 3]
Line 2,277 ⟶ 2,925:
> [ L1[], L2[] ]; # equivalent, just different syntax
[1, 2, 3, a, b, c]
</syntaxhighlight>
</lang>
 
=={{header|Mathcad}}==
Line 2,293 ⟶ 2,941:
augment concatenates arrays column-wise. The two (or more) arrays must have the same number of rows, and the resulting array column count is equal to the total number of columns in the augmented arrays.
 
<syntaxhighlight lang="mathcad">
<lang Mathcad>
create a pair of arbitrary array:
a:=matrix(2,2,max) b:=a+3
Line 2,309 ⟶ 2,957:
|1 1 3 4|
 
</syntaxhighlight>
</lang>
 
=={{header|Mathematica}} / {{header|Wolfram Language}}==
<langsyntaxhighlight Mathematicalang="mathematica">Join[{1,2,3}, {4,5,6}]
 
-> {1, 2, 3, 4, 5, 6}</langsyntaxhighlight>
 
=={{header|MATLAB}} / {{header|Octave}}==
Two arrays are concatenated by placing the two arrays between a pair of square brackets. A space between the two array names will concatenate them horizontally, and a semi-colon between array names will concatenate vertically.
<langsyntaxhighlight MATLABlang="matlab">>> a = [1 2 3];
>> b = [4 5 6];
>> c = [a b]
Line 2,326 ⟶ 2,974:
c =
1 2 3
4 5 6</langsyntaxhighlight>
 
For concatenation along higher dimensions, use cat():
<langsyntaxhighlight MATLABlang="matlab">>> a = randn([3 4 5]);
>> b = randn([3 4 7]);
>> c = cat(3,a,b);
>> size(c)
ans =
3 4 12</langsyntaxhighlight>
 
=={{header|Maxima}}==
<syntaxhighlight lang="text">u: [1, 2, 3, 4]$
v: [5, 6, 7, 8, 9, 10]$
append(u, v);
Line 2,359 ⟶ 3,007:
[1, 0, 0],
[0, 1, 0],
[0, 0, 1]) */</langsyntaxhighlight>
 
=={{header|Mercury}}==
 
<langsyntaxhighlight Mercurylang="mercury">A `append` B = C</langsyntaxhighlight>
 
It ''could'' be "as simple as array1 + array2", but the 'array' module names the operation 'append' rather than '+'. It's tempting to just say that Mercury supports ad-hoc polymorphism - it can infer that a bare '+' refers to 'float.+' or 'int.+' (or that the 'append' above is array.append, rather than list.append), by the types involved - but it also handles other ambiguities in the same way. For instance, Mercury (like Prolog and Erlang) treats the arity of a function as part of its name, where ''a(1, 2)'' and ''a(1)'' involve the distinct functions a/2 and a/1. But Mercury also (unlike Prolog and Erlang) supports [[currying]], where ''a(1)'' is a function that accepts a/2's second argument. So, is ''[a(X), a(Y), a(Z)]'' a list of whatever type a/1 evaluates to, or is it a list of curried a/2?
Line 2,369 ⟶ 3,017:
=={{header|min}}==
{{works with|min|0.19.3}}
<langsyntaxhighlight lang="min">(1 2 3) (4 "apple" 6) concat print</langsyntaxhighlight>
{{out}}
<pre>
Line 2,376 ⟶ 3,024:
 
=={{header|MiniScript}}==
<syntaxhighlight lang="miniscript">
<lang MiniScript>
arrOne = [1, 2, 3]
arrTwo = [4, 5, 6]
print arrOne + arrTwo
</syntaxhighlight>
</lang>
 
=={{header|Nanoquery}}==
Assuming a and b are array or list objects, they may concatenated using the '+' operator.
<syntaxhighlight lang Nanoquery="nanoquery">a + b</langsyntaxhighlight>
The '*' operator may also be used to create a specific number of copies of a list or array.
<pre>% a = list()
Line 2,394 ⟶ 3,042:
 
=={{header|Neko}}==
<syntaxhighlight lang="actionscript">/*
<lang ActionScript>/*
Array concatenation, in Neko
*/
Line 2,403 ⟶ 3,051:
/* $array(a1, a2) creates an array of two arrays, $aconcat merges to one */
var ac = $aconcat($array(a1, a2))
$print(ac, "\n")</langsyntaxhighlight>
 
{{out}}
Line 2,411 ⟶ 3,059:
 
=={{header|Nemerle}}==
<langsyntaxhighlight Nemerlelang="nemerle">using System.Console;
using Nemerle.Collections;
 
Line 2,422 ⟶ 3,070:
foreach (i in arr12) Write($"$i ");
}
}</langsyntaxhighlight>
 
=={{header|NetRexx}}==
NetRexx arrays are identical to [[Java|Java's]] so all the techniques described in the [[#Java|Java]] section apply to NetRexx too. This example uses the <tt>Collection</tt> classes to merge two arrays.
<langsyntaxhighlight lang="netrexx">/* NetRexx */
options replace format comments java crossref nobinary
 
Line 2,453 ⟶ 3,101:
loop m_ = 0 to merged.length - 1
say m_ merged[m_]
end m_</langsyntaxhighlight>
{{out}}
<pre>
Line 2,470 ⟶ 3,118:
 
=={{header|NewLISP}}==
<langsyntaxhighlight NewLISPlang="newlisp">; file: arraycon.lsp
; url: http://rosettacode.org/wiki/Array_concatenation
; author: oofoe 2012-01-28
Line 2,485 ⟶ 3,133:
(append '((x 56) (b 99)) '((z 34) (c 23) (r 88))))
 
(exit)</langsyntaxhighlight>
 
{{out}}
Line 2,496 ⟶ 3,144:
Examples tested to work with Q'Nial7
 
<langsyntaxhighlight Niallang="nial"> a:= 1 2 3
+-+-+-+
|1|2|3|
Line 2,503 ⟶ 3,151:
+-+-+-+
|4|5|6|
+-+-+-+</langsyntaxhighlight>
 
Table of lists:
 
<langsyntaxhighlight Niallang="nial"> a b
 
+-------+-------+
Line 2,513 ⟶ 3,161:
||1|2|3|||4|5|6||
|+-+-+-+|+-+-+-+|
+-------+-------+</langsyntaxhighlight>
 
Simple concatenation of two arrays/lists:
 
<langsyntaxhighlight Niallang="nial"> link a b
+-+-+-+-+-+-+
|1|2|3|4|5|6|
+-+-+-+-+-+-+</langsyntaxhighlight>
 
Convert list of lists to table:
 
<langsyntaxhighlight Niallang="nial"> mix a b
+-+-+-+
|1|2|3|
+-+-+-+
|4|5|6|
+-+-+-+</langsyntaxhighlight>
 
Interchange levels of a list of lists:
<langsyntaxhighlight Niallang="nial"> pack a b
+-----+-----+-----+
|+-+-+|+-+-+|+-+-+|
||1|4|||2|5|||3|6||
|+-+-+|+-+-+|+-+-+|
+-----+-----+-----+</langsyntaxhighlight>
 
=={{header|Nim}}==
Dynamic sized Sequences can simply be concatenated:
<langsyntaxhighlight lang="nim">var
x = @[1,2,3,4,5,6]
y = @[7,8,9,10,11]
z = x & y</langsyntaxhighlight>
 
Static sized Arrays:
<langsyntaxhighlight lang="nim">var
a = [1,2,3,4,5,6]
b = [7,8,9,10,11]
Line 2,553 ⟶ 3,201:
 
c[0..5] = a
c[6..10] = b</langsyntaxhighlight>
 
=={{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}}==
<langsyntaxhighlight lang="oberon2">
MODULE ArrayConcat;
IMPORT
Line 2,607 ⟶ 3,273:
END ArrayConcat.
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 2,614 ⟶ 3,280:
 
=={{header|Objeck}}==
<langsyntaxhighlight lang="objeck">
bundle Default {
class Arithmetic {
Line 2,646 ⟶ 3,312:
}
}
</syntaxhighlight>
</lang>
 
=={{header|Objective-C}}==
with immutable arrays:
<langsyntaxhighlight lang="objc">NSArray *arr1 = @[@1, @2, @3];
NSArray *arr2 = @[@4, @5, @6];
NSArray *arr3 = [arr1 arrayByAddingObjectsFromArray:arr2];</langsyntaxhighlight>
 
or adding onto a mutable array:
<langsyntaxhighlight lang="objc">NSArray *arr1 = @[@1, @2, @3];
NSArray *arr2 = @[@4, @5, @6];
NSMutableArray *arr3 = [NSMutableArray arrayWithArray:arr1];
[arr3 addObjectsFromArray:arr2];</langsyntaxhighlight>
 
=={{header|OCaml}}==
It is more natural in OCaml to use lists instead of arrays:
<langsyntaxhighlight lang="ocaml"># let list1 = [1; 2; 3];;
val list1 : int list = [1; 2; 3]
# let list2 = [4; 5; 6];;
val list2 : int list = [4; 5; 6]
# let list1and2 = list1 @ list2;;
val list1and2 : int list = [1; 2; 3; 4; 5; 6]</langsyntaxhighlight>
 
If you want to use arrays:
<langsyntaxhighlight lang="ocaml"># let array1 = [|1; 2; 3|];;
val array1 : int array = [|1; 2; 3|]
# let array2 = [|4; 5; 6|];;
val array2 : int array = [|4; 5; 6|]
# let array1and2 = Array.append array1 array2;;
val array1and2 : int array = [|1; 2; 3; 4; 5; 6|]</langsyntaxhighlight>
 
=={{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}}==
 
<langsyntaxhighlight Oforthlang="oforth">import: mapping
 
[1, 2, 3 ] [ 4, 5, 6, 7 ] + </langsyntaxhighlight>
 
=={{header|Onyx}}==
 
<langsyntaxhighlight lang="onyx"># With two arrays on the stack, cat pops
# them, concatenates them, and pushes the result back
# on the stack. This works with arrays of integers,
Line 2,697 ⟶ 3,395:
 
[1 true `a'] [2 false `b'] [`3rd array'] 3 ncat
# leaves [1 true `a' 2 false `b' `3rd array'] on the stack</langsyntaxhighlight>
 
=={{header|ooRexx}}==
<langsyntaxhighlight ooRexxlang="oorexx">a = .array~of(1,2,3)
say "Array a has " a~items "items"
b = .array~of(4,5,6)
say "Array b has " b~items "items"
a~appendall(b) -- adds all items from b to a
say "Array a now has " a~items "items"</langsyntaxhighlight>
{{out}}
<pre>Array a has 3 items
Line 2,713 ⟶ 3,411:
=={{header|Order}}==
Order supports two main aggregate types: tuples and sequences (similar to lists in other languages). Most "interesting" operations are limited to sequences, but both support an append operation, and each can be converted to the other.
<langsyntaxhighlight lang="c">#include <order/interpreter.h>
 
ORDER_PP( 8tuple_append(8tuple(1, 2, 3), 8tuple(4, 5, 6), 8pair(7, 8)) )
Line 2,719 ⟶ 3,417:
 
ORDER_PP( 8seq_append(8seq(1, 2, 3), 8seq(4, 5, 6), 8seq(7, 8)) )
// -> (1)(2)(3)(4)(5)(6)(7)(8)</langsyntaxhighlight>
 
=={{header|OxygenBasic}}==
<langsyntaxhighlight lang="oxygenbasic">
'CREATE DYNAMIC ARRAY SPACES USING STRINGS
 
Line 2,745 ⟶ 3,443:
 
print a[7] 'result 70
</syntaxhighlight>
</lang>
 
=={{header|Oz}}==
List are concatenated with <code>List.append</code> (shortcut: <code>Append</code>). Tuples are concatened with <code>Tuple.append</code>. Arrays do exist in Oz, but are rarely used.
<langsyntaxhighlight lang="oz">%% concatenating 2 lists
{Append [a b] [c d]} = [a b c d]
 
%% concatenating 2 tuples
{Tuple.append t(1 2 3) u(4 5 6)} = u(1 2 3 4 5 6)</langsyntaxhighlight>
 
=={{header|PARI/GP}}==
<syntaxhighlight lang ="parigp">concat(u,v)</langsyntaxhighlight>
 
=={{header|Pascal}}==
''See [[#Delphi|Delphi]] and [[#Free Pascal|Free Pascal]]''
 
=={{header|PascalABC.NET}}==
<syntaxhighlight lang="perl">
##
var a := |1,2,3,4|;
var b := Arr(5..8);
var c := a + b;
c.Println;
</syntaxhighlight>
{{out}}
<pre>
1 2 3 4 5 6 7 8
</pre>
 
=={{header|Perl}}==
In Perl, arrays placed into list context are flattened:
<langsyntaxhighlight lang="perl">my @arr1 = (1, 2, 3);
my @arr2 = (4, 5, 6);
my @arr3 = (@arr1, @arr2);</langsyntaxhighlight>
 
The <code>[http://perldoc.perl.org/functions/push.html push]</code> function appends elements onto an existing array:
<langsyntaxhighlight lang="perl">my @arr1 = (1, 2, 3);
my @arr2 = (4, 5, 6);
push @arr1, @arr2;
print "@arr1\n"; # prints "1 2 3 4 5 6"</langsyntaxhighlight>
 
=={{header|Phix}}==
{{libheader|Phix/basics}}
<!--<langsyntaxhighlight Phixlang="phix">-->
<span style="color: #004080;">sequence</span> <span style="color: #000000;">s1</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{<span style="color: #000000;">1<span style="color: #0000FF;">,<span style="color: #000000;">2<span style="color: #0000FF;">,<span style="color: #000000;">3<span style="color: #0000FF;">}<span style="color: #0000FF;">,</span> <span style="color: #000000;">s2</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{<span style="color: #000000;">4<span style="color: #0000FF;">,<span style="color: #000000;">5<span style="color: #0000FF;">,<span style="color: #000000;">6<span style="color: #0000FF;">}</span>
<span style="color: #0000FF;">?</span> <span style="color: #000000;">s1</span> <span style="color: #0000FF;">&</span> <span style="color: #000000;">s2
<!--</langsyntaxhighlight>-->
{{out}}
<pre>
Line 2,785 ⟶ 3,496:
 
=={{header|Phixmonti}}==
<langsyntaxhighlight Phixmontilang="phixmonti">1.0 "Hello" 3 2 / 4 2.1 power 4 tolist 5 6 7 8 4 tolist chain print</langsyntaxhighlight>
With syntactic sugar
<langsyntaxhighlight Phixmontilang="phixmonti">include ..\Utilitys.pmt
( 1.0 "Hello" 3 2 / 4 2.1 power ) ( 5 6 7 8 ) chain print</langsyntaxhighlight>
 
=={{header|PHP}}==
 
<langsyntaxhighlight lang="php">$arr1 = array(1, 2, 3);
$arr2 = array(4, 5, 6);
$arr3 = array_merge($arr1, $arr2);</langsyntaxhighlight>
 
=={{header|Picat}}==
Picat has support for both lists and arrays and arrays can be much faster. Some list functions works with both
lists and arrays, but some doesn't, e.g. append/3. In those cases one have to convert arrays to a list with to_list/1,
and back again with to_array/1.
 
<syntaxhighlight lang="picat">go =>
L1 = {1,2,3,4,5}, % define an array with {}
L2 = {6,7,8,9},
 
% The built-in array/list concatenation
println(L1 ++ L2),
 
% Using the built-in append/3 works only on lists
% so the arrays must be converted to lists.
append(L1.to_list,L2.to_list,L3),
println(L3.to_array),
nl.</syntaxhighlight>
 
{{out}}
<pre>{1,2,3,4,5,6,7,8,9}
{1,2,3,4,5,6,7,8,9}
</pre>
 
=={{header|PicoLisp}}==
Line 2,800 ⟶ 3,534:
 
There are destructive concatenations:
<langsyntaxhighlight PicoLisplang="picolisp">: (setq A (1 2 3) B '(a b c))
-> (a b c)
: (conc A B) # Concatenate lists in 'A' and 'B'
-> (1 2 3 a b c)
: A
-> (1 2 3 a b c) # Side effect: List in 'A' is modified!</langsyntaxhighlight>
and non-destructive concatenations:
<langsyntaxhighlight PicoLisplang="picolisp">: (setq A (1 2 3) B '(a b c))
-> (a b c)
: (append A B) # Append lists in 'A' and 'B'
Line 2,814 ⟶ 3,548:
-> (1 2 3)
: B
-> (a b c) # Arguments are not modified</langsyntaxhighlight>
 
=={{header|Pike}}==
<langsyntaxhighlight Pikelang="pike">int main() {
{
array arr1 = ({1, 2, 3});
array arr2 = ({4, 5, 6});
array arr3 = arr1 + arr2;
}</langsyntaxhighlight>
 
 
=={{header|PL/I}}==
Trivial example requires no computational statement.
Note that the arrays are not in static storage:
<syntaxhighlight lang="pl/i">
<lang PL/I>
declare x(12) fixed;
declare b(5) fixed defined x;
declare c(7) fixed defined x(1sub+5);
</syntaxhighlight>
</lang>
A more general example using dynamic bounds.
Again, no computation statement is required.
<syntaxhighlight lang="text">
declare x(m+n) fixed;
declare b(m) fixed defined x;
declare c(n) fixed defined x(1sub+hbound(b,1));
</syntaxhighlight>
</lang>
 
An alternative, that can be used to advantage for matrices
Line 2,846 ⟶ 3,578:
are used in the declarations, the bounds can be dynamic.
Matrix B is extended by placing matrix C on its diagonal:
<syntaxhighlight lang="text">
declare a(5,6) fixed;
declare b(3,4) fixed defined a(1sub, 2sub);
Line 2,861 ⟶ 3,593:
put skip list ('Composite matrix:');
put skip edit (a) ( skip, (hbound(a,2)) f(5,0) );
</syntaxhighlight>
</lang>
{{out}}
<syntaxhighlight lang="text">
Please type elements for a 3 x 4 matrix:
 
Line 2,878 ⟶ 3,610:
0 0 0 0 15 16
 
</syntaxhighlight>
</lang>
 
=={{header|Plain English}}==
Plain English has these functions for concatenating two sets of things:
<syntaxhighlight lang="text">
To append some things to some other things:
Put the things' first into a thing.
If the thing is nil, exit.
Remove the thing from the things.
Append the thing to the other things.
Repeat.
 
To prepend some things to some other things:
Get a thing from the things (backwards).
If the thing is nil, exit.
Remove the thing from the things.
Prepend the thing to the other things.
Repeat.
</syntaxhighlight>
 
=={{header|Pony}}==
<langsyntaxhighlight lang="pony">
actor Main
new create(env:Env)=>
Line 2,896 ⟶ 3,646:
env.out.print(i.string())
end
</syntaxhighlight>
</lang>
 
=={{header|PostScript}}==
{{libheader|initlib}}
<langsyntaxhighlight lang="postscript">
[1 2 3 4] [5 6 7 8] concat
</syntaxhighlight>
</lang>
 
=={{header|PowerShell}}==
<langsyntaxhighlight lang="powershell">$a = 1,2,3
$b = 4,5,6
 
$c = $a + $b
Write-Host $c</langsyntaxhighlight>
 
=={{header|Processing}}==
<langsyntaxhighlight lang="processing">
int[] a = {1, 2, 3}, b = {4, 5, 6};
 
int[] c = concat(a, b);
</syntaxhighlight>
</lang>
 
=={{header|Prolog}}==
<langsyntaxhighlight lang="prolog">
?- append([1,2,3],[4,5,6],R).
R = [1, 2, 3, 4, 5, 6].
</syntaxhighlight>
</lang>
 
=={{header|PureBasic}}==
<langsyntaxhighlight PureBasiclang="purebasic">Procedure displayArray(Array a(1), msg.s)
Protected i
Print(msg + " [")
Line 2,971 ⟶ 3,721:
Input()
CloseConsole()
EndIf</langsyntaxhighlight>
{{out}}
<pre>a: [5, 2, -4, -1, -2]
Line 2,980 ⟶ 3,730:
The <code>[http://docs.python.org/library/stdtypes.html#sequence-types-str-unicode-list-tuple-buffer-xrange +]</code> operator concatenates two lists and returns a new list.
The <code>[http://docs.python.org/library/stdtypes.html#mutable-sequence-types list.extend]</code> method appends elements of another list to the receiver.
<langsyntaxhighlight lang="python">arr1 = [1, 2, 3]
arr2 = [4, 5, 6]
arr3 = [7, 8, 9]
Line 2,986 ⟶ 3,736:
assert arr4 == [1, 2, 3, 4, 5, 6]
arr4.extend(arr3)
assert arr4 == [1, 2, 3, 4, 5, 6, 7, 8, 9]</langsyntaxhighlight>
 
Note: list.extend is normally accomplished using the += operator like this:
<langsyntaxhighlight lang="python">arr5 = [4, 5, 6]
arr6 = [7, 8, 9]
arr6 += arr5
assert arr6 == [7, 8, 9, 4, 5, 6]</langsyntaxhighlight>
 
=={{header|Q}}==
<langsyntaxhighlight lang="q">list1:1 2 3
list2:4 5 6
list1,list2</langsyntaxhighlight>
 
 
=={{header|QBasic}}==
{{works with|QBasic|1.1}}
{{works with|QuickBasic|4.5}}
<syntaxhighlight lang="qbasic">FUNCTION ConcatArrays(a(), b())
ta = UBOUND(a)
tb = UBOUND(b)
nt = ta + tb
FOR i = ta + 1 TO nt
a(i) = b(i - ta)
NEXT i
ConcatArrays = nt
END FUNCTION
 
dimen = 5
DIM a(dimen)
DIM b(dimen)
 
FOR i = 1 TO dimen
a(i) = i
b(i) = i + dimen
NEXT i
 
nt = ConcatArrays(a(), b())
 
FOR i = 1 TO nt
PRINT a(i);
IF i < nt THEN PRINT ", ";
NEXT i
</syntaxhighlight>
 
=={{header|QB64}}==
 
<syntaxhighlight lang="qb64">
 
Dim As Integer First, Second
First = 5: Second = 8
 
Dim As Integer Array1(1 To First), Array2(1 To Second), ArrayResult(1 To First + Second)
 
 
Init Array1(), 2
Print "First array"
ShowArr Array1()
Sleep 2
Print "Second array"
Init Array2(), 5
ShowArr Array2()
Sleep 2
Print "Final array"
 
ConcatArray Array1(), Array2(), ArrayResult()
ShowArr ArrayResult()
End
 
Sub Init (A() As Integer, R As Integer)
Dim Index As Integer
For Index = 1 To UBound(a)
A(Index) = Index * R
Next
End Sub
 
Sub ShowArr (A() As Integer)
Dim Index As Integer
For Index = 1 To UBound(a)
Print A(Index)
Next
End Sub
 
Sub ConcatArray (A() As Integer, B() As Integer, R() As Integer)
Dim Index As Integer
For Index = 1 To UBound(a)
R(Index) = A(Index)
Next
For Index = (1) To (UBound(b))
R(Index + UBound(a)) = B(Index)
Next
End Sub
 
</syntaxhighlight>
=={{header|Quackery}}==
 
Line 3,025 ⟶ 3,858:
=={{header|R}}==
 
<syntaxhighlight lang="r">
<lang R>
a1 <- c(1, 2, 3)
a2 <- c(3, 4, 5)
a3 <- c(a1, a2)
</syntaxhighlight>
</lang>
 
=={{header|Racket}}==
<langsyntaxhighlight lang="racket">
(vector-append #(1 2 3 4) #(5 6 7) #(8 9 10))
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 3,043 ⟶ 3,876:
(formerly Perl 6)
{{works with|Rakudo|2018.06}}
<syntaxhighlight lang="raku" perl6line>my @array1 = 1, 2, 3;
my @array2 = 4, 5, 6;
 
Line 3,060 ⟶ 3,893:
# of the second array to the first, use the .append method.
 
say @array1.append: @array2;</langsyntaxhighlight>
{{Out}}
<pre>[1 2 3 4 5 6]
Line 3,067 ⟶ 3,900:
 
=={{header|RapidQ}}==
<syntaxhighlight lang="vb">
<lang vb>
DEFINT A(1 to 4) = {1, 2, 3, 4}
DEFINT B(1 to 4) = {10, 20, 30, 40}
Line 3,074 ⟶ 3,907:
Redim A(1 to 8) as integer
MEMCPY(varptr(A(5)), varptr(B(1)), Sizeof(integer)*4)
</syntaxhighlight>
</lang>
 
=={{header|Rapira}}==
<syntaxhighlight lang="rapira">arr1 := <* 1, 2, 3 *>
arr2 := <* 4, 5, 6 *>
output: arr1 + arr2</syntaxhighlight>
 
=={{header|REBOL}}==
<syntaxhighlight lang="rebol">
<lang REBOL>
a1: [1 2 3]
a2: [4 5 6]
Line 3,085 ⟶ 3,923:
 
append/only a1 a3 ; -> [1 2 3 4 5 6 [7 8 9]]
</syntaxhighlight>
</lang>
 
=={{header|Red}}==
<langsyntaxhighlight Redlang="red">>> arr1: ["a" "b" "c"]
>> arr2: ["d" "e" "f"]
>> append arr1 arr2
Line 3,099 ⟶ 3,937:
== [22 33 44]
>> append/only arr1 arr4
== [1 2 3 "a" "b" "c" "d" "e" "f" [22 33 44]]</langsyntaxhighlight>
 
=={{header|ReScript}}==
<syntaxhighlight lang="rescript">Js.Array2.concat(["a", "b"], ["c", "d", "e"]) == ["a", "b", "c", "d", "e"]</syntaxhighlight>
 
=={{header|Retro}}==
<langsyntaxhighlight Retrolang="retro">{ #1 #2 #3 } { #4 #5 #6 } a:append</langsyntaxhighlight>
 
=={{header|REXX}}==
Line 3,115 ⟶ 3,956:
 
Consider:
<langsyntaxhighlight lang="rexx">a.1 = 10
a.2 = 22.7
a.7 = -12</langsyntaxhighlight>
where now we have three "elements", and they are disjointed (another word for ''sparse'').
<br>There are ways to handle this in REXX however.
Line 3,123 ⟶ 3,964:
<br>assuming that the stemmed variables are sequential.
<br><br>'''example:'''
<langsyntaxhighlight lang="rexx">fact.0=8
fact.1= 1
fact.2= 2
Line 3,131 ⟶ 3,972:
fact.6= 720
fact.7= 5040
fact.8=40320</langsyntaxhighlight>
To concat two "arrays" in REXX, the following assumes that the stemmed variables are in order, with no gaps, and none have a "null" value.
<langsyntaxhighlight lang="rexx">/*REXX program to demonstrates how to perform array concatenation.*/
 
p.= /*(below) a short list of primes.*/
Line 3,154 ⟶ 3,995:
say 'c.'m"="c.m /*show a "merged" C array nums.*/
end /*m*/
/*stick a fork in it, we're done.*/</langsyntaxhighlight>
{{out}}
<pre>
Line 3,185 ⟶ 4,026:
 
=={{header|Ring}}==
<langsyntaxhighlight lang="ring">
arr1 = [1, 2, 3]
arr2 = [4, 5, 6]
Line 3,194 ⟶ 4,035:
arr5 = arr4 + arr3
see arr5
</syntaxhighlight>
</lang>
 
=={{header|RLaB}}==
Line 3,200 ⟶ 4,041:
In RLaB the matrices can be appended (column-wise) or stacked (row-wise).
Consider few examples:
<syntaxhighlight lang="rlab">
<lang RLaB>
>> x = [1, 2, 3]
>> y = [4, 5, 6]
Line 3,214 ⟶ 4,055:
4 5 6
>>
</syntaxhighlight>
</lang>
 
=={{header|RPL}}==
In RPL, what is called arrays are actually vectors. Sets of numbers can be stored either in such data structures or in lists, depending on the planned use. Vectors are great for arithmetics, but lists are more versatile.
{{works with|Halcyon Calc|4.2.7}}
=== Vector concatenation===
≪ SWAP ARRY→ LIST→ DROP → n
≪ n 1 + ROLL ARRY→ LIST→ DROP
n + 1 →LIST →ARRY
≫ ≫ 'CONCAT' STO
 
 
[1 2 3] [4 5] CONCAT
{{out}}
<pre>
1: [1 2 3 4 5]
</pre>
A shorter version, without any local variable:
≪ SWAP ARRY→ 1 GET →LIST
SWAP ARRY→ 1 GET →LIST
+ LIST→ { } + →ARRY
≫ 'CONCAT' STO
 
=== List concatenation===
No need for a program to do that:
{1 2 3} {4 5} +
{{out}}
<pre>
1: {1 2 3 4 5}
</pre>
 
=={{header|Ruby}}==
The <code>[http://www.ruby-doc.org/core/classes/Array.html#M002209 Array#+]</code> method concatenates two arrays and returns a new array. The <code>[http://www.ruby-doc.org/core/classes/Array.html#M002166 Array#concat]</code> method appends elements of another array to the receiver.
<langsyntaxhighlight lang="ruby">arr1 = [1, 2, 3]
arr2 = [4, 5, 6]
arr3 = [7, 8, 9]
arr4 = arr1 + arr2 # => [1, 2, 3, 4, 5, 6]
arr4.concat(arr3) # => [1, 2, 3, 4, 5, 6, 7, 8, 9]</langsyntaxhighlight>
 
Or use flatten(1):
<langsyntaxhighlight lang="ruby">
# concat multiple arrays:
[arr1,arr2,arr3].flatten(1)
# ignore nil:
[arr1,arr2,arr3].compact.flatten(1)
</syntaxhighlight>
</lang>
 
=={{header|Rust}}==
<langsyntaxhighlight lang="rust">fn main() {
let a_vec = vec![1, 2, 3, 4, 5];
let b_vec = vec![6; 5];
Line 3,248 ⟶ 4,118:
concat
}
</syntaxhighlight>
</lang>
 
Or, with iterators:
 
<langsyntaxhighlight lang="rust">fn concatenate_arrays<T: Clone>(x: &[T], y: &[T]) -> Vec<T> {
x.iter().chain(y).cloned().collect()
}
</syntaxhighlight>
</lang>
 
=={{header|S-lang}}==
<langsyntaxhighlight Slang="s-lang">variable a = [1, 2, 3];
variable b = [4, 5, 6], c;</langsyntaxhighlight>
 
a+b is perfectly valid in S-Lang, but instead of the problem's desired effect,
Line 3,265 ⟶ 4,135:
But because arrays automatically 'flatten' when defined, concatenation is as
simple as:
<langsyntaxhighlight Slang="s-lang">c = [a, b];</langsyntaxhighlight>
Use of lists is more traditional; lists don't 'flatten', so we use either
list_concat() to create a new concatenated array:
<langsyntaxhighlight Slang="s-lang">a = {1, 2, 3};
b = {4, 5, 6};
c = list_concat(a, b);</langsyntaxhighlight>
 
or list_join():
<syntaxhighlight lang S="s-lang">list_join(a, b);</langsyntaxhighlight>
which adds the elements of b onto a.
 
=={{header|SASL}}==
In SASL, the concat operator ++ is built-in
<langsyntaxhighlight SASLlang="sasl">(1 2 3) ++ (4 5 6)</langsyntaxhighlight>
 
=={{header|Scala}}==
<langsyntaxhighlight Scalalang="scala">val arr1 = Array( 1, 2, 3 )
val arr2 = Array( 4, 5, 6 )
val arr3 = Array( 7, 8, 9 )
Line 3,288 ⟶ 4,158:
//or:
Array concat ( arr1, arr2, arr3 )
// res0: Array[Int] = Array(1, 2, 3, 4, 5, 6, 7, 8, 9)</langsyntaxhighlight>
 
=={{header|Scheme}}==
<langsyntaxhighlight lang="scheme">; in r5rs, there is append for lists, but we'll need to define vector-append
(define (vector-append . arg) (list->vector (apply append (map vector->list arg))))
 
(vector-append #(1 2 3 4) #(5 6 7) #(8 9 10))
; #(1 2 3 4 5 6 7 8 9 10)</langsyntaxhighlight>
 
''Note : vector-append is also defined in [http://srfi.schemers.org/srfi-43/srfi-43.html SRFI-43].''
Line 3,302 ⟶ 4,172:
{{works with|Gauche Scheme}}
 
<syntaxhighlight lang="scheme">
<lang Scheme>
(use gauche.array)
 
Line 3,328 ⟶ 4,198:
(print-matrix (array-concatenate a b))
(print-matrix (array-concatenate a b 1))
</syntaxhighlight>
</lang>
 
{{out}}
Line 3,345 ⟶ 4,215:
 
=={{header|Seed7}}==
<langsyntaxhighlight lang="seed7">$ include "seed7_05.s7i";
 
var array integer: a is [] (1, 2, 3, 4);
Line 3,360 ⟶ 4,230:
end for;
writeln;
end func;</langsyntaxhighlight>
 
{{out}}
Line 3,366 ⟶ 4,236:
 
=={{header|SenseTalk}}==
<langsyntaxhighlight lang="sensetalk">put (1, 2, 3) into list1
put (4, 5, 6) into list2
put list1 &&& list2 into list3
put list3</langsyntaxhighlight>
 
=={{header|SETL}}==
<langsyntaxhighlight lang="haskell">A := [1, 2, 3];
B := [3, 4, 5];
print(A + B); -- [1 2 3 3 4 5]</langsyntaxhighlight>
 
=={{header|Sidef}}==
<langsyntaxhighlight lang="ruby">var arr1 = [1, 2, 3];
var arr2 = [4, 5, 6];
var arr3 = (arr1 + arr2); # => [1, 2, 3, 4, 5, 6]</langsyntaxhighlight>
 
=={{header|Simula}}==
<langsyntaxhighlight lang="simula">BEGIN ! Concatenate arrays - of REAL, here;
 
CLASS REAL_ARRAY(N); INTEGER N;
Line 3,476 ⟶ 4,346:
X.CONCAT(NEW REAL_ARRAY(4)
.linearFill(-1, -3)).out(SYSOUT);
END.</langsyntaxhighlight>
{{out}}
<pre> 3.00 10.00 17.00 0.00 5.00 10.00 15.00
Line 3,485 ⟶ 4,355:
The binary operation of concatenation is made with the <tt>;</tt> (semi-colon) from the type Sequence. It is also available for appending Sequences to WriteStreams.
 
<langsyntaxhighlight lang="slate">
{1. 2. 3. 4. 5} ; {6. 7. 8. 9. 10}
</syntaxhighlight>
</lang>
 
=={{header|Slope}}==
 
<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}}==
Concatenation (appending) is made with the method <tt>,</tt> (comma), present in classes SequenceableCollection, ArrayedCollection and their subclasses (e.g. Array, String, OrderedCollection ...)
 
<langsyntaxhighlight lang="smalltalk">|a b c|
a := #(1 2 3 4 5).
b := #(6 7 8 9 10).
c := a,b.
c displayNl.</langsyntaxhighlight>
 
=={{header|SNOBOL4}}==
Line 3,504 ⟶ 4,388:
{{works with|CSnobol}}
 
<langsyntaxhighlight SNOBOL4lang="snobol4">* # Concatenate 2 arrays (vectors)
define('cat(a1,a2)i,j') :(cat_end)
cat cat = array(prototype(a1) + prototype(a2))
Line 3,523 ⟶ 4,407:
output = str2
output = str3
end</langsyntaxhighlight>
 
{{out}}
Line 3,529 ⟶ 4,413:
6 7 8 9 10
1 2 3 4 5 6 7 8 9 10</pre>
 
=={{header|SparForte}}==
As a structured script.
<syntaxhighlight lang="ada">#!/usr/local/bin/spar
pragma annotate( summary, "arraycat" )
@( description, "Show how to concatenate two arrays in your language." )
@( category, "tutorials" )
@( author, "Ken O. Burtch" )
@( see_also, "http://rosettacode.org/wiki/Array_concatenation" );
pragma license( unrestricted );
 
pragma software_model( nonstandard );
pragma restriction( no_external_commands );
 
procedure arraycat is
type arrayOf3 is array(1..3) of integer;
a1 : constant arrayOf3 := (1, 2, 3);
a2 : constant arrayOf3 := (4, 5, 6);
type arrayOf6 is array(1..6) of integer;
a3 : arrayOf6;
p : natural := arrays.first(a3);
begin
-- In SparForte, & only works on strings and there's no indefinite ranges
-- or array slicing. We have to do this the hard way, one element at a
-- time.
for i in arrays.first(a1)..arrays.last(a1) loop
a3(p) := a1(i);
p := @+1;
end loop;
for i in arrays.first(a2)..arrays.last(a2) loop
a3(p) := a2(i);
p := @+1;
end loop;
-- show the array
for i in arrays.first(a3)..arrays.last(a3) loop
put( a3(i) );
end loop;
new_line;
end arraycat;</syntaxhighlight>
 
=={{header|Standard ML}}==
<syntaxhighlight lang="standard ml">
<lang Standard ML>
val l1 = [1,2,3,4];;
val l2 = [5,6,7,8];;
val l3 = l1 @ l2 (* [1,2,3,4,5,6,7,8] *)
</syntaxhighlight>
</lang>
 
=={{header|Stata}}==
===Macro language===
<langsyntaxhighlight lang="stata">. matrix a=2,9,4\7,5,3\6,1,8
. matrix list a
 
Line 3,576 ⟶ 4,499:
r1 1 0 0
r2 0 1 0
r3 0 0 1</langsyntaxhighlight>
=== Mata ===
<langsyntaxhighlight lang="stata">. mata
: a=2,9,4\7,5,3\6,1,8
 
Line 3,602 ⟶ 4,525:
+-------------+
 
: end</langsyntaxhighlight>
 
=={{header|Swift}}==
<langsyntaxhighlight Swiftlang="swift">let array1 = [1,2,3]
let array2 = [4,5,6]
let array3 = array1 + array2</langsyntaxhighlight>
 
=={{header|Tailspin}}==
<langsyntaxhighlight lang="tailspin">
def a: [1, 2, 3];
def b: [4, 5, 6];
[$a..., $b...] -> !OUT::write
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 3,621 ⟶ 4,544:
 
=={{header|Tcl}}==
<langsyntaxhighlight lang="tcl">set a {1 2 3}
set b {4 5 6}
set ab [concat $a $b]; # 1 2 3 4 5 6</langsyntaxhighlight>
Note that in the Tcl language, “arrays” are hash maps of strings to variables, so the notion of concatenation doesn't really apply. What other languages (usually) call arrays are “lists” in Tcl.
 
Line 3,656 ⟶ 4,579:
 
=={{header|Trith}}==
<langsyntaxhighlight lang="trith">[1 2 3] [4 5 6] concat</langsyntaxhighlight>
 
=={{header|UNIX Shell}}==
Line 3,664 ⟶ 4,587:
{{works with|bash}}
 
<langsyntaxhighlight lang="bash">array1=( 1 2 3 4 5 )
array2=( 6 7 8 9 10 )
botharrays=( ${array1[@]} ${array2[@]} )</langsyntaxhighlight>
 
Whitespace-delimited strings work in much the same way:
Line 3,672 ⟶ 4,595:
{{works with|bash}}
 
<langsyntaxhighlight lang="bash">array1='1 2 3 4 5'
array2='6 7 8 9 10'
 
Line 3,679 ⟶ 4,602:
 
# Concatenated to a string ...
botharrays_s="$array1 $array2"</langsyntaxhighlight>
 
=={{header|Ursa}}==
<langsyntaxhighlight lang="ursa"># create two streams (the ursa equivalent of arrays)
# a contains the numbers 1-10, b contains 11-20
decl int<> a b
Line 3,697 ⟶ 4,620:
 
# output a to the console
out a endl console</langsyntaxhighlight>
 
=={{header|Vala}}==
<langsyntaxhighlight lang="vala">int[] array_concat(int[]a,int[]b){
int[] c = new int[a.length + b.length];
Memory.copy(c, a, a.length * sizeof(int));
Line 3,713 ⟶ 4,636:
stdout.printf("%d\n",i);
}
}</langsyntaxhighlight>
 
=={{header|VBA}}==
 
<syntaxhighlight lang="vb">
<lang vb>
Option Explicit
 
Line 3,743 ⟶ 4,666:
Concat_Array = TmpA1
End Function
</syntaxhighlight>
</lang>
{{out}}
<pre>With Array 1 : 1, 2, 3, 4, 5, 24/11/2017, azerty
Line 3,751 ⟶ 4,674:
 
=={{header|VBScript}}==
<langsyntaxhighlight lang="vb">Function ArrayConcat(arr1, arr2)
ReDim ret(UBound(arr1) + UBound(arr2) + 1)
For i = 0 To UBound(arr1)
Line 3,768 ⟶ 4,691:
WScript.Echo "arr2 = array(" & Join(arr2,", ") & ")"
arr3 = ArrayConcat(arr1, arr2)
WScript.Echo "arr1 + arr2 = array(" & Join(arr3,", ") & ")"</langsyntaxhighlight>
 
{{out}}
Line 3,778 ⟶ 4,701:
 
=={{header|Visual Basic .NET}}==
<langsyntaxhighlight lang="vbnet">
Dim iArray1() As Integer = {1, 2, 3}
Dim iArray2() As Integer = {4, 5, 6}
Line 3,784 ⟶ 4,707:
 
iArray3 = iArray1.Concat(iArray2).ToArray
</syntaxhighlight>
</lang>
 
=={{header|V (Vlang)}}==
V (Vlang) uses a '''<<''' operator for array concatenation. Destination array needs to be mutable.
 
<syntaxhighlight lang="go">// V, array concatenation
// Tectonics: v run array-concatenation.v
module main
 
// starts here
pub fn main() {
mut arr1 := [1,2,3,4]
arr2 := [5,6,7,8]
 
arr1 << arr2
println(arr1)
}</syntaxhighlight>
 
{{out}}<pre>$ v run array-concatenation.v
[1, 2, 3, 4, 5, 6, 7, 8]</pre>
 
=={{header|Wart}}==
Line 3,790 ⟶ 4,732:
Wart doesn't have arrays yet, just lists.
 
<langsyntaxhighlight lang="wart">a <- '(1 2 3)
b <- '(4 5 6)
a+b
# => (1 2 3 4 5 6)</langsyntaxhighlight>
 
=={{header|Wren}}==
<langsyntaxhighlight ecmascriptlang="wren">var arr1 = [1,2,3]
var arr2 = [4,5,6]
System.print(arr1 + arr2)</langsyntaxhighlight>
{{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}}==
<langsyntaxhighlight Yabasiclang="yabasic">sub arrayConcatenation(a(), b())
local ta, tb, nt, i
Line 3,838 ⟶ 4,813:
if i < nt print ", ";
next i
print</langsyntaxhighlight>
 
=={{header|Yacas}}==
<langsyntaxhighlight Yacaslang="yacas">Concat({1,2,3}, {4,5,6})
 
Out> {1, 2, 3, 4, 5, 6}</langsyntaxhighlight>
 
=={{header|Yorick}}==
<langsyntaxhighlight lang="yorick">a = [1,2,3];
b = [4,5,6];
ab = grow(a, b);</langsyntaxhighlight>
 
=={{header|Z80 Assembly}}==
The routine <code>Monitor_Memdump</code> displays a hexdump to the Amstrad CPC's screen.
Credit to Keith of [https://www.chibiakumas.com ChibiAkumas] for creating it.
 
<syntaxhighlight lang="z80"> org $8000
 
ld hl,TestArray1 ; pointer to first array
ld de,ArrayRam ; pointer to ram area
ld bc,6 ; size of first array
ldir
; DE is already adjusted past the last entry
; of the first array
ld hl,TestArray2 ; pointer to second array
ld bc,4 ; size of second array
ldir
call Monitor_MemDump
db 32 ; hexdump 32 bytes (only the bytes from the arrays will be shown in the output for clarity)
dw ArrayRam ; start dumping from ArrayRam
ret ; return to basic
 
ArrayRam:
ds 24,0 ;24 bytes of ram initialized to zero
 
org $9000
TestArray2:
byte $23,$45,$67,$89
; just to prove that this doesn't rely on the arrays
; being "already concatenated" I've stored them
; in the reverse order.
TestArray1:
byte $aa,$bb,$cc,$dd,$ee,$ff</syntaxhighlight>
 
{{out}}
<pre>
801D:
AA BB CC DD EE FF 23 45 67 89
</pre>
 
=={{header|Zig}}==
There are no hidden memory allocations in Zig.
<syntaxhighlight lang="zig">
const std = @import("std");
 
pub fn main() !void {
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
defer _ = gpa.deinit();
 
const allocator = gpa.allocator();
 
var array1 = [_]u32{ 1, 2, 3, 4, 5 };
var array2 = [_]u32{ 6, 7, 8, 9, 10, 11, 12 };
 
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}}==
Lists (both mutable and read only), no built in support for numeric vectors/arrays/matrices
<langsyntaxhighlight lang="zkl">T(1,2).extend(T(4,5,6)) //-->L(1,2,4,5,6)
T(1,2).extend(4,5,6) //-->L(1,2,4,5,6)</langsyntaxhighlight>
 
=={{header|zonnon}}==
<syntaxhighlight lang="text">
module Main;
import
Line 3,931 ⟶ 4,983:
WriteAry(Concat2(x,y));
end Main.
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 3,940 ⟶ 4,992:
=={{header|Zsh}}==
Concatenating arrays.
<langsyntaxhighlight lang="zsh">a=(1 2 3)
b=(a b c)
 
c=($a $b)</langsyntaxhighlight>
Pushing a single element into an array.
<langsyntaxhighlight lang="zsh">a+=4</langsyntaxhighlight>
Pushing another array into an array.
<langsyntaxhighlight lang="zsh">a+=($b)</langsyntaxhighlight>
 
=={{header|ZX Spectrum Basic}}==
{{trans|Liberty BASIC}}
<langsyntaxhighlight lang="zxbasic">10 LET x=10
20 LET y=20
30 DIM a(x)
Line 3,965 ⟶ 5,017:
130 PRINT c(i);", ";
140 NEXT i
</syntaxhighlight>
</lang>
92

edits