Loop over multiple arrays simultaneously: Difference between revisions
Content added Content deleted
imported>Maxima enthusiast No edit summary |
(→{{header|Zig}}: Add more examples and annotate version) |
||
Line 4,834: | Line 4,834: | ||
=={{header|Zig}}== |
=={{header|Zig}}== |
||
===Limit by minimum length=== |
|||
'''Works with''': 0.10.x |
|||
<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 = std.math.min3(arr1.len, arr2.len, arr3.len); |
|||
⚫ | |||
⚫ | |||
} |
|||
}</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 (arr1) |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"); |
<syntaxhighlight lang="zig">const std = @import("std"); |
||
const |
const arr1 = [_]u8{ 'a', 'b', 'c' }; |
||
const |
const arr2 = [_]u8{ 'A', 'B', 'C' }; |
||
const |
const arr3 = [_]u8{ '1', '2', '3' }; |
||
pub fn main() !void { |
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> |
}</syntaxhighlight> |