Loop over multiple arrays simultaneously: Difference between revisions

m
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 56.0x :
<syntaxhighlight lang="elena">import system'routines;
import extensions;
Line 1,753 ⟶ 1,763:
var a3 := new int[]{1,2,3};
for(int i := 0,; i < a1.Length,; i += 1)
{
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="ecmascriptwren">var a1 = ["a", "b", "c"]
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 a1: []const u8arr1 = &[_]u8{ 'a', 'b', 'c' };
const a2: []const u8arr2 = &[_]u8{ 'A', 'B', 'C' };
const a3: []const u8arr3 = &[_]u8{ '1', '2', '3' };
 
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 std.io.getStdOut().writer()stdout_w.print("{c} {c} {dc}\n", .{ a1[i]arr1_e, a2arr2[i], a3arr3[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();
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();
for (a1arr1) |_arr1_e, i| {
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>
Anonymous user