Loop over multiple arrays simultaneously: Difference between revisions

m
imported>Arakov
(10 intermediate revisions by 10 users not shown)
Line 701:
 
=={{header|BASIC}}==
==={{header|Applesoft BASIC}}===
{{trans|ZX Spectrum Basic}}
<syntaxhighlight lang="basic">REM DEFINE THE ARRAYS AND POPULATE THEM
0 SIZE = 3: DIM A$(SIZE),B$(SIZE),C(SIZE): FOR I = 1 TO SIZE:A$(I) = CHR$ (96 + I):B$(I) = CHR$ (64 + I):C(I) = I: NEXT
 
REM LOOP OVER MULTIPLE ARRAYS SIMULTANEOUSLY
1 FOR I = 1 TO SIZE
2 PRINT A$(I)B$(I)C(I)
3 NEXT I
</syntaxhighlight>
==={{header|BaCon}}===
<syntaxhighlight lang="freebasic">
Line 1,010 ⟶ 1,020:
130 IF l <= szc THEN READ c$(l): PRINT c$(l);
140 PRINT: REM newline
150145 NEXT l
150 PRINT "The arrays are shown in columns."
160 PRINT "A$ runs down the left hand side,"
Line 1,572 ⟶ 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,733 ⟶ 1,753:
 
=={{header|Elena}}==
ELENA 56.0x :
<syntaxhighlight lang="elena">import system'routines;
import extensions;
Line 1,743 ⟶ 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,763 ⟶ 1,783:
.zipBy(a3, (first,second => first + second.toString() ));
zipped.forEach::(e)
{ console.writeLine:e };
Line 1,973 ⟶ 1,993:
bB2
cC3
</pre>
 
=={{header|FutureBasic}}==
<syntaxhighlight lang="futurebasic">
void local fn DoIt
CFArrayRef a1 = @[@"a",@"b",@"c"]
CFArrayRef a2 = @[@"A",@"B",@"C"]
CFArrayRef a3 = @[@"1",@"2",@"3"]
long i, count = len(a1)
for i = 0 to count - 1
print a1[i]a2[i]a3[i]
next
end fn
 
fn DoIt
 
HandleEvents
</syntaxhighlight>
{{output}}
<pre>
aA1
bB2
Cc3
</pre>
 
Line 2,120 ⟶ 2,164:
 
<syntaxhighlight lang="haskell">{-# LANGUAGE ParallelListComp #-}
 
main = sequence [ putStrLn [x, y, z] | x <- "abc" | y <- "ABC" | z <- "123"]</syntaxhighlight>
main :: IO [()]
main =
sequence
[ putStrLn [x, y, z]
| x <- "abc"
| y <- "ABC"
| z <- "123"
]</syntaxhighlight>
 
'''Using Transpose'''
Line 2,225 ⟶ 2,277:
[http://www.cs.arizona.edu/icon/library/procs/numbers.htm Uses max from
numbers]
 
=={{header|Insitux}}==
 
{{Trans|Clojure}}
 
<syntaxhighlight lang="insitux">(map str "abc" "ABC" "123")
["aA1" "bB2" "cC3"]</syntaxhighlight>
 
<syntaxhighlight lang="insitux">(map str ["a" "b" "c"] ["A" "B" "C"] ["1" "2" "3"])
["aA1" "bB2" "cC3"]</syntaxhighlight>
 
=={{header|J}}==
Line 2,903 ⟶ 2,965:
<syntaxhighlight lang="mathematica">MapThread[Print, {{"a", "b", "c"}, {"A", "B", "C"}, {1, 2, 3}}];</syntaxhighlight>
All arguments must be lists of the same length.
 
=={{header|Maxima}}==
<syntaxhighlight lang="maxima">
/* Function that loops over multiple arrays simultaneously depending on the list with less length */
lomas(L):=block(
minlen:lmin(map(length,L)),
makelist(makelist(L[i][j],i,1,length(L)),j,1,minlen))$
 
/* Test case */
lst:[[a,b,c],[A,B,C],[1,2,3]]$
lomas(lst);
</syntaxhighlight>
{{out}}
<pre>
[[a,A,1],[b,B,2],[c,C,3]]
</pre>
 
=={{header|Mercury}}==
Line 3,811 ⟶ 3,889:
next
</syntaxhighlight>
 
=={{header|RPL}}==
===1993+ versions===
≪ 3 ≪ + + ≫ DOLIST
OBJ→ DROP
≫ '<span style="color:blue">CONCAT3</span>' STO
 
{ "a" "b" "c" } { "A" "B" "C" } { "1" "2" "3" } <span style="color:blue">CONCAT3</span>
{{out}}
<pre>
3: "aA1"
2: "bB2"
1: "cC3"
</pre>
===Older versions===
≪ → a b c
≪ 1 a SIZE '''FOR''' j
a j GET b j GET c j GET + +
'''NEXT'''
≫ ≫'<span style="color:blue">CONCAT3</span>' STO
 
=={{header|Ruby}}==
Line 4,597 ⟶ 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,746 ⟶ 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 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();
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");
 
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