Array concatenation: Difference between revisions
m
→{{header|Binary Lambda Calculus}}
imported>Belowdecent |
|||
(13 intermediate revisions by 9 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="cobolfree">IDENTIFICATION DIVISION.
PROGRAM-ID. array-concat.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 table-one.
05 int-field PIC 999 OCCURS 0 TO 5 TIMES DEPENDING ON t1.
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.
array-concat-main.
PERFORM initialize-tables
PERFORM concatenate-tables
PERFORM
GOBACK.
initialize-tables.
MOVE 4 TO t1
PERFORM VARYING tally FROM 1 BY 1
COMPUTE int-field OF
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.
PERFORM
PERFORM
DISPLAY FUNCTION
DISPLAY FUNCTION
END
{{out}}
<pre>
6, 12, 18, 3, 6, 9, 12
</pre>
Line 1,831 ⟶ 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>
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,587 ⟶ 2,712:
=={{header|Liberty BASIC}}==
{{works with|Just BASIC}}
{{works with|Run BASIC}}
<syntaxhighlight lang="lb"> x=10
y=20
Line 3,075 ⟶ 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,228 ⟶ 3,373:
fmt.println(xy)
}</syntaxhighlight>
Line 4,205 ⟶ 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,571 ⟶ 4,725:
=={{header|Wren}}==
<syntaxhighlight lang="
var arr2 = [4,5,6]
System.print(arr1 + arr2)</syntaxhighlight>
Line 4,701 ⟶ 4,855:
=={{header|Zig}}==
There are no hidden memory allocations in Zig.
<syntaxhighlight lang="zig">
pub fn main() !void {
var
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}}==
|