Anonymous user
Loop over multiple arrays simultaneously: Difference between revisions
Loop over multiple arrays simultaneously (view source)
Revision as of 11:39, 25 January 2024
, 3 months ago→{{header|Elena}}
imported>Maxima enthusiast No edit summary |
imported>Arakov |
||
(4 intermediate revisions by 4 users not shown) | |||
Line 1,582:
(This will stop when the end of the shortest collection is reached.)
=={{header|EasyLang}}==
<syntaxhighlight>
a$[] = [ "a" "b" "c" ]
b$[] = [ "A" "B" "C" ]
c[] = [ 1 2 3 ]
for i = 1 to 3
print a$[i] & b$[i] & c[i]
.
</syntaxhighlight>
=={{header|EchoLisp}}==
Line 1,743 ⟶ 1,753:
=={{header|Elena}}==
ELENA
<syntaxhighlight lang="elena">import system'routines;
import extensions;
Line 1,753 ⟶ 1,763:
var a3 := new int[]{1,2,3};
for(int i := 0
{
console.printLine(a1[i], a2[i], a3[i])
Line 1,773 ⟶ 1,783:
.zipBy(a3, (first,second => first + second.toString() ));
zipped.forEach::(e)
{ console.writeLine:e };
Line 4,685 ⟶ 4,695:
=={{header|Wren}}==
The following script will work as expected provided the lengths of a1 and a2 are at least equal to the length of a3. Otherwise it will produce a 'Subscript out of bounds' error.
<syntaxhighlight lang="
var a2 = ["A", "B", "C"]
var a3 = [1, 2, 3]
Line 4,834 ⟶ 4,844:
=={{header|Zig}}==
===Limit by minimum length===
'''Works with''': 0.10.x
<syntaxhighlight lang="zig">const std = @import("std");
const
const
const
pub fn main() std.fs.File.WriteError!void {
const stdout = std.io.getStdOut();
for (a1) |_, i|▼
const stdout_w = stdout.writer();
try std.io.getStdOut().writer().print("{c} {c} {d}\n", .{ a1[i], a2[i], a3[i] });▼
const n = std.math.min3(arr1.len, arr2.len, arr3.len);
for (arr1[0..n]) |arr1_e, i| {
▲ try
}
}</syntaxhighlight>
'''Works with''': 0.11.x, 0.12.0-dev.1381+61861ef39
<syntaxhighlight lang="zig">const std = @import("std");
const arr1 = [_]u8{ 'a', 'b', 'c' };
const arr2 = [_]u8{ 'A', 'B', 'C' };
const arr3 = [_]u8{ '1', '2', '3' };
pub fn main() std.fs.File.WriteError!void {
const stdout = std.io.getStdOut();
const stdout_w = stdout.writer();
const n = @min(arr1.len, arr2.len, arr3.len);
for (arr1[0..n], arr2[0..n], arr3[0..n]) |arr1_e, arr2_e, arr3_e| {
try stdout_w.print("{c} {c} {c}\n", .{ arr1_e, arr2_e, arr3_e });
}
}</syntaxhighlight>
===Limit by length of first array===
'''Works with''': 0.10.x
This example will print up-to arr1 length (asserts that other arrays are at least that long).
<syntaxhighlight lang="zig">const std = @import("std");
const arr1 = [_]u8{ 'a', 'b', 'c' };
const arr2 = [_]u8{ 'A', 'B', 'C' };
const arr3 = [_]u8{ '1', '2', '3' };
pub fn main() std.fs.File.WriteError!void {
const stdout = std.io.getStdOut();
const stdout_w = stdout.writer();
try stdout_w.print("{c} {c} {c}\n", .{ arr1_e, arr2[i], arr3[i] });
}
}</syntaxhighlight>
'''Works with''': 0.11.x, 0.12.0-dev.1381+61861ef39
<syntaxhighlight lang="zig">const std = @import("std");
const arr1 = [_]u8{ 'a', 'b', 'c' };
const arr2 = [_]u8{ 'A', 'B', 'C' };
const arr3 = [_]u8{ '1', '2', '3' };
pub fn main() std.fs.File.WriteError!void {
const stdout = std.io.getStdOut();
const stdout_w = stdout.writer();
for (arr1, 0..) |arr1_e, i| {
try stdout_w.print("{c} {c} {c}\n", .{ arr1_e, arr2[i], arr3[i] });
}
}</syntaxhighlight>
===Assert that arrays have equal length===
'''Works with''': 0.11.x, 0.12.0-dev.1381+61861ef39
This example will print up-to arr1 length (asserts that other arrays are exactly that long => asserts that lengths are equal).
<syntaxhighlight lang="zig">const std = @import("std");
const arr1 = [_]u8{ 'a', 'b', 'c' };
const arr2 = [_]u8{ 'A', 'B', 'C' };
const arr3 = [_]u8{ '1', '2', '3' };
pub fn main() std.fs.File.WriteError!void {
const stdout = std.io.getStdOut();
const stdout_w = stdout.writer();
for (arr1, arr2, arr3) |arr1_e, arr2_e, arr3_e| {
try stdout_w.print("{c} {c} {c}\n", .{ arr1_e, arr2_e, arr3_e });
}
}</syntaxhighlight>
|