Loop over multiple arrays simultaneously: Difference between revisions
Content added Content deleted
m (→{{header|Haskell}}: ZipList version tidied for legibility) |
Thundergnat (talk | contribs) m (syntax highlighting fixup automation) |
||
Line 41: | Line 41: | ||
=={{header|11l}}== |
=={{header|11l}}== |
||
< |
<syntaxhighlight lang="11l">L(x, y, z) zip(‘abc’, ‘ABC’, ‘123’) |
||
print(x‘’y‘’z)</ |
print(x‘’y‘’z)</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 52: | Line 52: | ||
=={{header|360 Assembly}}== |
=={{header|360 Assembly}}== |
||
< |
<syntaxhighlight lang="360asm">* Loop over multiple arrays simultaneously 09/03/2017 |
||
LOOPSIM CSECT |
LOOPSIM CSECT |
||
USING LOOPSIM,R12 base register |
USING LOOPSIM,R12 base register |
||
Line 75: | Line 75: | ||
PG DC CL80' ' buffer |
PG DC CL80' ' buffer |
||
YREGS |
YREGS |
||
END LOOPSIM</ |
END LOOPSIM</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 103: | Line 103: | ||
and if they are not, it will simply read from the wrong addresses. |
and if they are not, it will simply read from the wrong addresses. |
||
< |
<syntaxhighlight lang="8080asm"> org 100h |
||
lxi b,0 ; Let (B)C be the array index |
lxi b,0 ; Let (B)C be the array index |
||
outer: lxi d,As ; Use DE to walk the array-of-arrays |
outer: lxi d,As ; Use DE to walk the array-of-arrays |
||
Line 148: | Line 148: | ||
Alen: equ $-A3 |
Alen: equ $-A3 |
||
;;; Zero-terminated array-of-arrays |
;;; Zero-terminated array-of-arrays |
||
As: dw A1,A2,A3,0</ |
As: dw A1,A2,A3,0</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 170: | Line 170: | ||
base addresses of the arrays into <code>bx</code> one by one. |
base addresses of the arrays into <code>bx</code> one by one. |
||
< |
<syntaxhighlight lang="asm"> cpu 8086 |
||
bits 16 |
bits 16 |
||
org 100h |
org 100h |
||
Line 200: | Line 200: | ||
;;; Array of arrays |
;;; Array of arrays |
||
As: dw A1,A2,A3 |
As: dw A1,A2,A3 |
||
Aslen: equ ($-As)/2 ; Length of array of arrays (in words)</ |
Aslen: equ ($-As)/2 ; Length of array of arrays (in words)</syntaxhighlight> |
||
Line 212: | Line 212: | ||
=={{header|ACL2}}== |
=={{header|ACL2}}== |
||
< |
<syntaxhighlight lang="lisp">(defun print-lists (xs ys zs) |
||
(if (or (endp xs) (endp ys) (endp zs)) |
(if (or (endp xs) (endp ys) (endp zs)) |
||
nil |
nil |
||
Line 223: | Line 223: | ||
(rest zs))))) |
(rest zs))))) |
||
(print-lists '("a" "b" "c") '(A B C) '(1 2 3))</ |
(print-lists '("a" "b" "c") '(A B C) '(1 2 3))</syntaxhighlight> |
||
=={{header|Action!}}== |
=={{header|Action!}}== |
||
< |
<syntaxhighlight lang="action!">PROC Main() |
||
CHAR ARRAY a="abc",b="ABC" |
CHAR ARRAY a="abc",b="ABC" |
||
BYTE ARRAY c=[1 2 3] |
BYTE ARRAY c=[1 2 3] |
||
Line 235: | Line 235: | ||
PrintF("%C%C%B%E",a(i+1),b(i+1),c(i)) |
PrintF("%C%C%B%E",a(i+1),b(i+1),c(i)) |
||
OD |
OD |
||
RETURN</ |
RETURN</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Loop_over_multiple_arrays_simultaneously.png Screenshot from Atari 8-bit computer] |
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Loop_over_multiple_arrays_simultaneously.png Screenshot from Atari 8-bit computer] |
||
Line 245: | Line 245: | ||
=={{header|Ada}}== |
=={{header|Ada}}== |
||
< |
<syntaxhighlight lang="ada">with Ada.Text_IO; use Ada.Text_IO; |
||
procedure Array_Loop_Test is |
procedure Array_Loop_Test is |
||
Line 257: | Line 257: | ||
(Index))(2)); |
(Index))(2)); |
||
end loop; |
end loop; |
||
end Array_Loop_Test;</ |
end Array_Loop_Test;</syntaxhighlight> |
||
=={{header|ALGOL 68}}== |
=={{header|ALGOL 68}}== |
||
Line 271: | Line 271: | ||
1.8-8d] - due to extensive use of '''format'''[ted] ''transput''}} |
1.8-8d] - due to extensive use of '''format'''[ted] ''transput''}} |
||
< |
<syntaxhighlight lang="algol68">[]UNION(CHAR,INT) x=("a","b","c"), y=("A","B","C"), |
||
z=(1,2,3); |
z=(1,2,3); |
||
FOR i TO UPB x DO |
FOR i TO UPB x DO |
||
printf(($ggd$, x[i], y[i], z[i], $l$)) |
printf(($ggd$, x[i], y[i], z[i], $l$)) |
||
OD</ |
OD</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 284: | Line 284: | ||
=={{header|ALGOL W}}== |
=={{header|ALGOL W}}== |
||
< |
<syntaxhighlight lang="algolw">begin |
||
% declare the three arrays % |
% declare the three arrays % |
||
string(1) array a, b ( 1 :: 3 ); |
string(1) array a, b ( 1 :: 3 ); |
||
Line 294: | Line 294: | ||
% loop over the arrays % |
% loop over the arrays % |
||
for i := 1 until 3 do write( i_w := 1, s_w := 0, a(i), b(i), c(i) ); |
for i := 1 until 3 do write( i_w := 1, s_w := 0, a(i), b(i), c(i) ); |
||
end. </ |
end. </syntaxhighlight> |
||
If the arrays are not the same length, a subscript range error would occur when a non-existant element was accessed. |
If the arrays are not the same length, a subscript range error would occur when a non-existant element was accessed. |
||
Line 307: | Line 307: | ||
vectors, zeroes for numeric vectors) to match the longest vector. |
vectors, zeroes for numeric vectors) to match the longest vector. |
||
<lang |
<syntaxhighlight lang="apl">f ← ↓∘⍉∘↑</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 323: | Line 323: | ||
If we have a generic Applescript '''map''' function, we can use it to write a generic '''zipListsWith''', which applies a given function over lists derived from the nth members of an arbitrary list of (equal-length) lists. (Where lists are of uneven length, items beyond the maximum shared length are ignored). |
If we have a generic Applescript '''map''' function, we can use it to write a generic '''zipListsWith''', which applies a given function over lists derived from the nth members of an arbitrary list of (equal-length) lists. (Where lists are of uneven length, items beyond the maximum shared length are ignored). |
||
< |
<syntaxhighlight lang="applescript">-- ZIP LISTS WITH FUNCTION --------------------------------------------------- |
||
-- zipListsWith :: ([a] -> b) -> [[a]] -> [[b]] |
-- zipListsWith :: ([a] -> b) -> [[a]] -> [[b]] |
||
Line 413: | Line 413: | ||
end script |
end script |
||
end if |
end if |
||
end mReturn</ |
end mReturn</syntaxhighlight> |
||
{{Out}} |
{{Out}} |
||
<pre>aA1 |
<pre>aA1 |
||
Line 421: | Line 421: | ||
But a transpose function might be simpler: |
But a transpose function might be simpler: |
||
< |
<syntaxhighlight lang="applescript">-- CONCAT MAPPED OVER A TRANSPOSITION ---------------------------------------- |
||
on run |
on run |
||
Line 495: | Line 495: | ||
on unlines(xs) |
on unlines(xs) |
||
intercalate(linefeed, xs) |
intercalate(linefeed, xs) |
||
end unlines</ |
end unlines</syntaxhighlight> |
||
{{Out}} |
{{Out}} |
||
<pre>aA1 |
<pre>aA1 |
||
Line 503: | Line 503: | ||
=={{header|Arturo}}== |
=={{header|Arturo}}== |
||
< |
<syntaxhighlight lang="rebol">parts: ["abc" "ABC" [1 2 3]] |
||
loop 0..2 'x -> |
loop 0..2 'x -> |
||
print ~"|parts\0\[x]||parts\1\[x]||parts\2\[x]|"</ |
print ~"|parts\0\[x]||parts\1\[x]||parts\2\[x]|"</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 518: | Line 518: | ||
[http://www.autohotkey.com/docs/commands/StringSplit.htm StringSplit] |
[http://www.autohotkey.com/docs/commands/StringSplit.htm StringSplit] |
||
creates a pseudo-array |
creates a pseudo-array |
||
< |
<syntaxhighlight lang="autohotkey">List1 = a,b,c |
||
List2 = A,B,C |
List2 = A,B,C |
||
List3 = 1,2,3 |
List3 = 1,2,3 |
||
Line 543: | Line 543: | ||
Result .= List1_%A_Index% List2_%A_Index% List3_%A_Index% "`n" |
Result .= List1_%A_Index% List2_%A_Index% List3_%A_Index% "`n" |
||
Return, Result |
Return, Result |
||
}</ |
}</syntaxhighlight> |
||
An array that is too short on creation will return empty strings when |
An array that is too short on creation will return empty strings when |
||
trying to retrieve further elements. The 2nd Message box shows: |
trying to retrieve further elements. The 2nd Message box shows: |
||
Line 556: | Line 556: | ||
([http://l.autohotkey.com/docs/Objects.htm Objects]) and the |
([http://l.autohotkey.com/docs/Objects.htm Objects]) and the |
||
[http://l.autohotkey.net/docs/commands/For.htm For loop]. |
[http://l.autohotkey.net/docs/commands/For.htm For loop]. |
||
< |
<syntaxhighlight lang="ahk">List1 := ["a", "b", "c"] |
||
List2 := ["A", "B", "C"] |
List2 := ["A", "B", "C"] |
||
List3 := [ 1 , 2 , 3 ] |
List3 := [ 1 , 2 , 3 ] |
||
Line 572: | Line 572: | ||
Result .= value . List2[key] . List3[key] "`n" |
Result .= value . List2[key] . List3[key] "`n" |
||
Return, Result |
Return, Result |
||
}</ |
}</syntaxhighlight> |
||
The output from this script is identical to the first one. |
The output from this script is identical to the first one. |
||
=={{header|AWK}}== |
=={{header|AWK}}== |
||
< |
<syntaxhighlight lang="awk">BEGIN { |
||
split("a,b,c", a, ","); |
split("a,b,c", a, ","); |
||
split("A,B,C", b, ","); |
split("A,B,C", b, ","); |
||
Line 584: | Line 584: | ||
print a[i] b[i] c[i]; |
print a[i] b[i] c[i]; |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
=={{header|Axe}}== |
=={{header|Axe}}== |
||
Line 590: | Line 590: | ||
L₃ for simplicity. In practice, one would want to arrange the arrays to |
L₃ for simplicity. In practice, one would want to arrange the arrays to |
||
all fit within L₁ to avoid volatility issues with L₂ and L₃. |
all fit within L₁ to avoid volatility issues with L₂ and L₃. |
||
< |
<syntaxhighlight lang="axe">'a'→{L₁} |
||
'b'→{L₁+1} |
'b'→{L₁+1} |
||
'c'→{L₁+2} |
'c'→{L₁+2} |
||
Line 601: | Line 601: | ||
For(I,0,2) |
For(I,0,2) |
||
Disp {L₁+I}►Char,{L₂+I}►Char,{L₃+I}►Dec,i |
Disp {L₁+I}►Char,{L₂+I}►Char,{L₃+I}►Dec,i |
||
End</ |
End</syntaxhighlight> |
||
=={{header|Babel}}== |
=={{header|Babel}}== |
||
Line 607: | Line 607: | ||
First, you could transpose the lists: |
First, you could transpose the lists: |
||
< |
<syntaxhighlight lang="babel">main: { (('a' 'b' 'c')('A' 'B' 'C')('1' '2' '3')) |
||
simul_array } |
simul_array } |
||
simul_array!: |
simul_array!: |
||
{ trans |
{ trans |
||
{ { << } each "\n" << } each }</ |
{ { << } each "\n" << } each }</syntaxhighlight> |
||
The 'trans' operator substitutes nil in the portions of each transposed |
The 'trans' operator substitutes nil in the portions of each transposed |
||
Line 624: | Line 624: | ||
each list using a user-defined cdrall operator: |
each list using a user-defined cdrall operator: |
||
< |
<syntaxhighlight lang="babel">main: { (('a' 'b' 'c')('A' 'B' 'C')('1' '2' '3')) |
||
simul_array } |
simul_array } |
||
Line 642: | Line 642: | ||
{ zap 0 last } |
{ zap 0 last } |
||
{ nil } |
{ nil } |
||
if} each }</ |
if} each }</syntaxhighlight> |
||
This solution is formally identical to the first and will handle lists |
This solution is formally identical to the first and will handle lists |
||
Line 652: | Line 652: | ||
=={{header|BaCon}}== |
=={{header|BaCon}}== |
||
< |
<syntaxhighlight lang="freebasic"> |
||
DECLARE a1$[] = {"a", "b", "c"} TYPE STRING |
DECLARE a1$[] = {"a", "b", "c"} TYPE STRING |
||
DECLARE a2$[] = {"A", "B", "C"} TYPE STRING |
DECLARE a2$[] = {"A", "B", "C"} TYPE STRING |
||
Line 662: | Line 662: | ||
INCR i |
INCR i |
||
WEND |
WEND |
||
</ |
</syntaxhighlight> |
||
=={{header|BASIC256}}== |
=={{header|BASIC256}}== |
||
{{trans|FreeBASIC}} |
{{trans|FreeBASIC}} |
||
< |
<syntaxhighlight lang="basic256">dim arr1$(3) : arr1$ = {"a", "b", "c"} |
||
dim arr2$(3) : arr2$ = {"A", "B", "C"} |
dim arr2$(3) : arr2$ = {"A", "B", "C"} |
||
dim arr3(3) : arr3 = {1, 2, 3} |
dim arr3(3) : arr3 = {1, 2, 3} |
||
Line 674: | Line 674: | ||
print arr1$[i]; arr2$[i]; arr3[i] |
print arr1$[i]; arr2$[i]; arr3[i] |
||
next i |
next i |
||
end</ |
end</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 684: | Line 684: | ||
=={{header|BBC BASIC}}== |
=={{header|BBC BASIC}}== |
||
< |
<syntaxhighlight lang="bbcbasic"> DIM array1$(2), array2$(2), array3%(2) |
||
array1$() = "a", "b", "c" |
array1$() = "a", "b", "c" |
||
array2$() = "A", "B", "C" |
array2$() = "A", "B", "C" |
||
Line 691: | Line 691: | ||
FOR index% = 0 TO 2 |
FOR index% = 0 TO 2 |
||
PRINT array1$(index%) ; array2$(index%) ; array3%(index%) |
PRINT array1$(index%) ; array2$(index%) ; array3%(index%) |
||
NEXT</ |
NEXT</syntaxhighlight> |
||
=={{header|Beads}}== |
=={{header|Beads}}== |
||
This solution accounts for arrays of varying lengths, and if they are interspersed with undefined characters by replacing them with spaces. |
This solution accounts for arrays of varying lengths, and if they are interspersed with undefined characters by replacing them with spaces. |
||
< |
<syntaxhighlight lang="beads">beads 1 program 'Loop over multiple arrays simultaneously' |
||
calc main_init |
calc main_init |
||
const |
const |
||
Line 703: | Line 703: | ||
const largest = max(tree_hi(x), tree_hi(y), tree_hi(z)) |
const largest = max(tree_hi(x), tree_hi(y), tree_hi(z)) |
||
loop reps:largest count:i //where u_cc defines what to use for undefined characters |
loop reps:largest count:i //where u_cc defines what to use for undefined characters |
||
log to_str(x[i], u_cc:' ') & to_str(y[i], u_cc:' ') & to_str(z[i], u_cc:' ')</ |
log to_str(x[i], u_cc:' ') & to_str(y[i], u_cc:' ') & to_str(z[i], u_cc:' ')</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>aA1 |
<pre>aA1 |
||
Line 714: | Line 714: | ||
For arrays of differing lengths, you'd need to manually check for an out-of-range index and deal with it appropriately. |
For arrays of differing lengths, you'd need to manually check for an out-of-range index and deal with it appropriately. |
||
< |
<syntaxhighlight lang="befunge">0 >:2g,:3g,:4gv |
||
@_^#`2:+1,+55,< |
@_^#`2:+1,+55,< |
||
abc |
abc |
||
ABC |
ABC |
||
123</ |
123</syntaxhighlight> |
||
=={{header|C}}== |
=={{header|C}}== |
||
Line 727: | Line 727: | ||
application-specific way. |
application-specific way. |
||
< |
<syntaxhighlight lang="c">#include <stdio.h> |
||
char a1[] = {'a','b','c'}; |
char a1[] = {'a','b','c'}; |
||
Line 737: | Line 737: | ||
printf("%c%c%i\n", a1[i], a2[i], a3[i]); |
printf("%c%c%i\n", a1[i], a2[i], a3[i]); |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
(Note: Some compilers may require a flag to accept this modern C code, |
(Note: Some compilers may require a flag to accept this modern C code, |
||
Line 750: | Line 750: | ||
=={{header|C sharp|C#}}== |
=={{header|C sharp|C#}}== |
||
< |
<syntaxhighlight lang="csharp">class Program |
||
{ |
{ |
||
static void Main(string[] args) |
static void Main(string[] args) |
||
Line 762: | Line 762: | ||
Console.WriteLine("{0}{1}{2}", a[i], b[i], c[i]); |
Console.WriteLine("{0}{1}{2}", a[i], b[i], c[i]); |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
Using Enumerable.Zip (stops when either source runs out of elements): |
Using Enumerable.Zip (stops when either source runs out of elements): |
||
< |
<syntaxhighlight lang="csharp"> |
||
int[] numbers = { 1, 2, 3, 4 }; |
int[] numbers = { 1, 2, 3, 4 }; |
||
string[] words = { "one", "two", "three" }; |
string[] words = { "one", "two", "three" }; |
||
Line 773: | Line 773: | ||
second)); |
second)); |
||
</syntaxhighlight> |
|||
</lang> |
|||
Like how a perl programmer would write it (still using Zip): |
Like how a perl programmer would write it (still using Zip): |
||
< |
<syntaxhighlight lang="csharp"> |
||
Console.WriteLine((new[] { 1, 2, 3, 4 }).Zip(new[] { "a", "b", "c" }, |
Console.WriteLine((new[] { 1, 2, 3, 4 }).Zip(new[] { "a", "b", "c" }, |
||
(f, s) => f + " " + s)); |
(f, s) => f + " " + s)); |
||
</syntaxhighlight> |
|||
</lang> |
|||
Custom implementation for arrays of different lengths that pads with spaces after the end of the shorter arrays: |
Custom implementation for arrays of different lengths that pads with spaces after the end of the shorter arrays: |
||
< |
<syntaxhighlight lang="csharp"> |
||
public static void Multiloop(char[] A, char[] B, int[] C) |
public static void Multiloop(char[] A, char[] B, int[] C) |
||
{ |
{ |
||
Line 792: | Line 792: | ||
Console.WriteLine($"{(i < A.Length ? A[i] : ' ')}, {(i < B.Length ? B[i] : ' ')}, {(i < C.Length ? C[i] : ' ')}"); |
Console.WriteLine($"{(i < A.Length ? A[i] : ' ')}, {(i < B.Length ? B[i] : ' ')}, {(i < C.Length ? C[i] : ' ')}"); |
||
} |
} |
||
</syntaxhighlight> |
|||
</lang> |
|||
usage: |
usage: |
||
< |
<syntaxhighlight lang="csharp">Multiloop(new char[] { 'a', 'b', 'c', 'd' }, new char[] { 'A', 'B', 'C' }, new int[] { 1, 2, 3, 4, 5 });</syntaxhighlight> |
||
=={{header|C++}}== |
=={{header|C++}}== |
||
With <code>std::vector</code>s: |
With <code>std::vector</code>s: |
||
< |
<syntaxhighlight lang="cpp">#include <iostream> |
||
#include <vector> |
#include <vector> |
||
Line 816: | Line 816: | ||
std::cout << *lIt << *uIt << *nIt << "\n"; |
std::cout << *lIt << *uIt << *nIt << "\n"; |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
Using static arrays: |
Using static arrays: |
||
< |
<syntaxhighlight lang="cpp">#include <iostream> |
||
int main(int argc, char* argv[]) |
int main(int argc, char* argv[]) |
||
Line 835: | Line 835: | ||
"\n"; |
"\n"; |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
===C++11=== |
===C++11=== |
||
With <code>std::vector</code>s: |
With <code>std::vector</code>s: |
||
< |
<syntaxhighlight lang="cpp">#include <iostream> |
||
#include <vector> |
#include <vector> |
||
Line 859: | Line 859: | ||
std::cout << *ilow << *iup << *inum << "\n"; |
std::cout << *ilow << *iup << *inum << "\n"; |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
Using static arrays: |
Using static arrays: |
||
< |
<syntaxhighlight lang="cpp">#include <iostream> |
||
#include <iterator> |
#include <iterator> |
||
Line 882: | Line 882: | ||
std::cout << *ilow << *iup << *inum << "\n"; |
std::cout << *ilow << *iup << *inum << "\n"; |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
With <code>std::array</code>s: |
With <code>std::array</code>s: |
||
< |
<syntaxhighlight lang="cpp">#include <iostream> |
||
#include <array> |
#include <array> |
||
Line 905: | Line 905: | ||
std::cout << *ilow << *iup << *inum << "\n"; |
std::cout << *ilow << *iup << *inum << "\n"; |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
With <code>std::array</code>s by indexes: |
With <code>std::array</code>s by indexes: |
||
< |
<syntaxhighlight lang="cpp">#include <iostream> |
||
#include <array> |
#include <array> |
||
#include <algorithm> |
#include <algorithm> |
||
Line 930: | Line 930: | ||
std::cout << lowers[i] << uppers[i] << nums[i] << "\n"; |
std::cout << lowers[i] << uppers[i] << nums[i] << "\n"; |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
===C++23=== |
===C++23=== |
||
< |
<syntaxhighlight lang="cpp">#include <array> |
||
#include <ranges> |
#include <ranges> |
||
#include <format> |
#include <format> |
||
Line 947: | Line 947: | ||
std::cout << std::format("{}{}{}\n", x, y, z); |
std::cout << std::format("{}{}{}\n", x, y, z); |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
=={{header|Chapel}}== |
=={{header|Chapel}}== |
||
< |
<syntaxhighlight lang="chapel">var a1 = [ "a", "b", "c" ]; |
||
var a2 = [ "A", "B", "C" ]; |
var a2 = [ "A", "B", "C" ]; |
||
var a3 = [ 1, 2, 3 ]; |
var a3 = [ 1, 2, 3 ]; |
||
for (x,y,z) in zip(a1, a2, a3) do |
for (x,y,z) in zip(a1, a2, a3) do |
||
writeln(x,y,z);</ |
writeln(x,y,z);</syntaxhighlight> |
||
=={{header|Clojure}}== |
=={{header|Clojure}}== |
||
< |
<syntaxhighlight lang="clojure">(doseq [s (map #(str %1 %2 %3) "abc" "ABC" "123")] |
||
(println s))</ |
(println s))</syntaxhighlight> |
||
The sequence stops when the shortest list is exhausted. |
The sequence stops when the shortest list is exhausted. |
||
< |
<syntaxhighlight lang="clojure"> |
||
(apply map str ["abc" "ABC" "123"]) |
(apply map str ["abc" "ABC" "123"]) |
||
("aA1" "bB2" "cC3") |
("aA1" "bB2" "cC3") |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|COBOL}}== |
=={{header|COBOL}}== |
||
< |
<syntaxhighlight lang="cobol"> IDENTIFICATION DIVISION. |
||
PROGRAM-ID. Loop-Over-Multiple-Tables. |
PROGRAM-ID. Loop-Over-Multiple-Tables. |
||
Line 992: | Line 992: | ||
GOBACK |
GOBACK |
||
.</ |
.</syntaxhighlight> |
||
=={{header|Common Lisp}}== |
=={{header|Common Lisp}}== |
||
=== Using functional application === |
=== Using functional application === |
||
< |
<syntaxhighlight lang="lisp">(mapc (lambda (&rest args) |
||
(format t "~{~A~}~%" args)) |
(format t "~{~A~}~%" args)) |
||
'(|a| |b| |c|) |
'(|a| |b| |c|) |
||
'(a b c) |
'(a b c) |
||
'(1 2 3))</ |
'(1 2 3))</syntaxhighlight> |
||
If lists are different lengths, it stops after the shortest one. |
If lists are different lengths, it stops after the shortest one. |
||
=== Using LOOP === |
=== Using LOOP === |
||
< |
<syntaxhighlight lang="lisp"> |
||
(loop for x in '("a" "b" "c") |
(loop for x in '("a" "b" "c") |
||
for y in '(a b c) |
for y in '(a b c) |
||
for z in '(1 2 3) |
for z in '(1 2 3) |
||
do (format t "~a~a~a~%" x y z)) |
do (format t "~a~a~a~%" x y z)) |
||
</syntaxhighlight> |
|||
</lang> |
|||
=== Using DO === |
=== Using DO === |
||
< |
<syntaxhighlight lang="lisp"> |
||
(do ((x '("a" "b" "c") (rest x)) ; |
(do ((x '("a" "b" "c") (rest x)) ; |
||
(y '("A" "B" "C" "D") (rest y)) ; |
(y '("A" "B" "C" "D") (rest y)) ; |
||
Line 1,019: | Line 1,019: | ||
((or (null x) (null y) (null z))) ; Break condition |
((or (null x) (null y) (null z))) ; Break condition |
||
(format t "~a~a~a~%" (first x) (first y) (first z))) ; On every loop print first elements |
(format t "~a~a~a~%" (first x) (first y) (first z))) ; On every loop print first elements |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
Line 1,029: | Line 1,029: | ||
=={{header|D}}== |
=={{header|D}}== |
||
< |
<syntaxhighlight lang="d">import std.stdio, std.range; |
||
void main () { |
void main () { |
||
foreach (a, b, c; zip("abc", "ABC", [1, 2, 3])) |
foreach (a, b, c; zip("abc", "ABC", [1, 2, 3])) |
||
writeln(a, b, c); |
writeln(a, b, c); |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>aA1 |
<pre>aA1 |
||
Line 1,042: | Line 1,042: | ||
On default it stops when the shortest range is exhausted |
On default it stops when the shortest range is exhausted |
||
(same as StoppingPolicy.shortest): |
(same as StoppingPolicy.shortest): |
||
< |
<syntaxhighlight lang="d">import std.stdio, std.range; |
||
void main () { |
void main () { |
||
Line 1,062: | Line 1,062: | ||
foreach (p; zip(sp.requireSameLength, a1, a2)) |
foreach (p; zip(sp.requireSameLength, a1, a2)) |
||
writeln(p.tupleof); |
writeln(p.tupleof); |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>11 |
<pre>11 |
||
Line 1,077: | Line 1,077: | ||
There is also std.range.lockstep: |
There is also std.range.lockstep: |
||
< |
<syntaxhighlight lang="d">import std.stdio, std.range; |
||
void main() { |
void main() { |
||
Line 1,091: | Line 1,091: | ||
foreach (index, a, b; lockstep(arr1, arr2)) |
foreach (index, a, b; lockstep(arr1, arr2)) |
||
writefln("Index %s: a = %s, b = %s", index, a, b); |
writefln("Index %s: a = %s, b = %s", index, a, b); |
||
}</ |
}</syntaxhighlight> |
||
Lower level code that stops at the shortest length: |
Lower level code that stops at the shortest length: |
||
< |
<syntaxhighlight lang="d">import std.stdio, std.algorithm; |
||
void main () { |
void main () { |
||
Line 1,102: | Line 1,102: | ||
foreach (i; 0 .. min(s1.length, s2.length, a1.length)) |
foreach (i; 0 .. min(s1.length, s2.length, a1.length)) |
||
writeln(s1[i], s2[i], a1[i]); |
writeln(s1[i], s2[i], a1[i]); |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>aA1 |
<pre>aA1 |
||
Line 1,108: | Line 1,108: | ||
=={{header|Delphi}}== |
=={{header|Delphi}}== |
||
< |
<syntaxhighlight lang="delphi">program LoopOverArrays; |
||
{$APPTYPE CONSOLE} |
{$APPTYPE CONSOLE} |
||
Line 1,125: | Line 1,125: | ||
Readln; |
Readln; |
||
end.</ |
end.</syntaxhighlight> |
||
=={{header|Diego}}== |
=={{header|Diego}}== |
||
< |
<syntaxhighlight lang="diego">set_namespace(rosettacode); |
||
add_mat(myMatrix)_row(a,b,c)_row(A,B,C)_row(1,2,3); |
add_mat(myMatrix)_row(a,b,c)_row(A,B,C)_row(1,2,3); |
||
Line 1,139: | Line 1,139: | ||
me_msg([output]); |
me_msg([output]); |
||
reset_namespace[];</ |
reset_namespace[];</syntaxhighlight> |
||
Diego has no issue when arrays are of a different length, the "missing" array entries will be handled as empty. Note, the <code>matrix</code> will become a <code>clump</code>, but can still be treated as a <code>matrix</code>. |
Diego has no issue when arrays are of a different length, the "missing" array entries will be handled as empty. Note, the <code>matrix</code> will become a <code>clump</code>, but can still be treated as a <code>matrix</code>. |
||
< |
<syntaxhighlight lang="diego">set_ns(rosettacode); |
||
add_clump(myClump)_row(a,b,c,d)_row(A,B,C,D,E,F)_row(-1,0,1,2,3); // The default spread is presumed to be 'origin' |
add_clump(myClump)_row(a,b,c,d)_row(A,B,C,D,E,F)_row(-1,0,1,2,3); // The default spread is presumed to be 'origin' |
||
Line 1,153: | Line 1,153: | ||
me_msg([output]); |
me_msg([output]); |
||
reset_ns[];</ |
reset_ns[];</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 1,169: | Line 1,169: | ||
element. |
element. |
||
< |
<syntaxhighlight lang="delphi">const a1 = ['a', 'b', 'c']; |
||
const a2 = ['A', 'B', 'C']; |
const a2 = ['A', 'B', 'C']; |
||
const a3 = [1, 2, 3]; |
const a3 = [1, 2, 3]; |
||
Line 1,175: | Line 1,175: | ||
var i : Integer; |
var i : Integer; |
||
for i := 0 to 2 do |
for i := 0 to 2 do |
||
PrintLn(Format('%s%s%d', [a1[i], a2[i], a3[i]]));</ |
PrintLn(Format('%s%s%d', [a1[i], a2[i], a3[i]]));</syntaxhighlight> |
||
=={{header|E}}== |
=={{header|E}}== |
||
Line 1,184: | Line 1,184: | ||
indexes as keys, so a not entirely awful idiom exists: |
indexes as keys, so a not entirely awful idiom exists: |
||
< |
<syntaxhighlight lang="e">def a1 := ["a","b","c"] |
||
def a2 := ["A","B","C"] |
def a2 := ["A","B","C"] |
||
def a3 := ["1","2","3"] |
def a3 := ["1","2","3"] |
||
Line 1,190: | Line 1,190: | ||
for i => v1 in a1 { |
for i => v1 in a1 { |
||
println(v1, a2[i], a3[i]) |
println(v1, a2[i], a3[i]) |
||
}</ |
}</syntaxhighlight> |
||
This will obviously fail if a2 or a3 are shorter than a1, and omit items |
This will obviously fail if a2 or a3 are shorter than a1, and omit items |
||
Line 1,197: | Line 1,197: | ||
Given a parallel iteration utility, we might write this: |
Given a parallel iteration utility, we might write this: |
||
< |
<syntaxhighlight lang="e">for [v1, v2, v3] in zip(a1, a2, a3) { |
||
println(v1, v2, v3) |
println(v1, v2, v3) |
||
}</ |
}</syntaxhighlight> |
||
<code>zip</code> cannot yet be defined for all collections |
<code>zip</code> cannot yet be defined for all collections |
||
Line 1,209: | Line 1,209: | ||
collections. |
collections. |
||
< |
<syntaxhighlight lang="e">def zip { |
||
to run(l1, l2) { |
to run(l1, l2) { |
||
def zipped { |
def zipped { |
||
Line 1,236: | Line 1,236: | ||
zipped |
zipped |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
(This will stop when the end of the shortest collection is reached.) |
(This will stop when the end of the shortest collection is reached.) |
||
=={{header|EchoLisp}}== |
=={{header|EchoLisp}}== |
||
< |
<syntaxhighlight lang="scheme"> |
||
;; looping over different sequences : infinite stream, string, list and vector |
;; looping over different sequences : infinite stream, string, list and vector |
||
;; loop stops as soon a one sequence ends. |
;; loop stops as soon a one sequence ends. |
||
Line 1,256: | Line 1,256: | ||
1004 "E" 4 s |
1004 "E" 4 s |
||
1005 "F" 5 t |
1005 "F" 5 t |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Efene}}== |
=={{header|Efene}}== |
||
< |
<syntaxhighlight lang="efene">@public |
||
run = fn () { |
run = fn () { |
||
lists.foreach(fn ((A, B, C)) { io.format("~s~n", [[A, B, C]]) }, |
lists.foreach(fn ((A, B, C)) { io.format("~s~n", [[A, B, C]]) }, |
||
lists.zip3("abc", "ABC", "123")) |
lists.zip3("abc", "ABC", "123")) |
||
} |
} |
||
</syntaxhighlight> |
|||
</lang> |
|||
If the lists are not all the same length, an error is thrown. |
If the lists are not all the same length, an error is thrown. |
||
=={{header|Eiffel}}== |
=={{header|Eiffel}}== |
||
< |
<syntaxhighlight lang="eiffel"> |
||
example (a_array: READABLE_INDEXABLE [BOUNDED [ANY]]): STRING |
example (a_array: READABLE_INDEXABLE [BOUNDED [ANY]]): STRING |
||
-- Assemble output for a 2-dim array in `a_array' |
-- Assemble output for a 2-dim array in `a_array' |
||
Line 1,303: | Line 1,303: | ||
>> |
>> |
||
end |
end |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
aA1 |
aA1 |
||
Line 1,323: | Line 1,323: | ||
=={{header|Ela}}== |
=={{header|Ela}}== |
||
< |
<syntaxhighlight lang="ela">open monad io list imperative |
||
xs = zipWith3 (\x y z -> show x ++ show y ++ show z) ['a','b','c'] |
xs = zipWith3 (\x y z -> show x ++ show y ++ show z) ['a','b','c'] |
||
Line 1,334: | Line 1,334: | ||
return $ each print xss |
return $ each print xss |
||
print_and_calc xs ::: IO</ |
print_and_calc xs ::: IO</syntaxhighlight> |
||
The code above can be written shorter. First there is no need in lists |
The code above can be written shorter. First there is no need in lists |
||
Line 1,341: | Line 1,341: | ||
composition operator: |
composition operator: |
||
< |
<syntaxhighlight lang="ela">xs = zipWith3 (\x -> (x++) >> (++)) "abc" "ABC" |
||
"123"</ |
"123"</syntaxhighlight> |
||
=={{header|Elena}}== |
=={{header|Elena}}== |
||
ELENA 5.0 : |
ELENA 5.0 : |
||
< |
<syntaxhighlight lang="elena">import system'routines; |
||
import extensions; |
import extensions; |
||
Line 1,361: | Line 1,361: | ||
console.readChar() |
console.readChar() |
||
}</ |
}</syntaxhighlight> |
||
Using zipBy extension: |
Using zipBy extension: |
||
< |
<syntaxhighlight lang="elena">import system'routines. |
||
import extensions. |
import extensions. |
||
Line 1,379: | Line 1,379: | ||
console.readChar(); |
console.readChar(); |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 1,389: | Line 1,389: | ||
=={{header|Elixir}}== |
=={{header|Elixir}}== |
||
'''string list:''' |
'''string list:''' |
||
< |
<syntaxhighlight lang="elixir">l1 = ["a", "b", "c"] |
||
l2 = ["A", "B", "C"] |
l2 = ["A", "B", "C"] |
||
l3 = ["1", "2", "3"] |
l3 = ["1", "2", "3"] |
||
IO.inspect List.zip([l1,l2,l3]) |> Enum.map(fn x-> Tuple.to_list(x) |> Enum.join end) |
IO.inspect List.zip([l1,l2,l3]) |> Enum.map(fn x-> Tuple.to_list(x) |> Enum.join end) |
||
#=> ["aA1", "bB2", "cC3"]</ |
#=> ["aA1", "bB2", "cC3"]</syntaxhighlight> |
||
'''char_list:''' |
'''char_list:''' |
||
< |
<syntaxhighlight lang="elixir">l1 = 'abc' |
||
l2 = 'ABC' |
l2 = 'ABC' |
||
l3 = '123' |
l3 = '123' |
||
IO.inspect List.zip([l1,l2,l3]) |> Enum.map(fn x-> Tuple.to_list(x) end) |
IO.inspect List.zip([l1,l2,l3]) |> Enum.map(fn x-> Tuple.to_list(x) end) |
||
#=> ['aA1', 'bB2', 'cC3']</ |
#=> ['aA1', 'bB2', 'cC3']</syntaxhighlight> |
||
When the length of the list is different: |
When the length of the list is different: |
||
< |
<syntaxhighlight lang="elixir">iex(1)> List.zip(['abc','ABCD','12345']) |> Enum.map(&Tuple.to_list(&1)) |
||
['aA1', 'bB2', 'cC3'] |
['aA1', 'bB2', 'cC3'] |
||
iex(2)> List.zip(['abcde','ABC','12']) |> Enum.map(&Tuple.to_list(&1)) |
iex(2)> List.zip(['abcde','ABC','12']) |> Enum.map(&Tuple.to_list(&1)) |
||
['aA1', 'bB2']</ |
['aA1', 'bB2']</syntaxhighlight> |
||
The zipping finishes as soon as any enumerable completes. |
The zipping finishes as soon as any enumerable completes. |
||
=={{header|Erlang}}== |
=={{header|Erlang}}== |
||
Shortest option: |
Shortest option: |
||
< |
<syntaxhighlight lang="erlang">lists:zipwith3(fun(A,B,C)-> |
||
io:format("~s~n",[[A,B,C]]) end, "abc", "ABC", "123").</ |
io:format("~s~n",[[A,B,C]]) end, "abc", "ABC", "123").</syntaxhighlight> |
||
However, as every expression in Erlang has to return something, printing text returns 'ok'. A list with as many 'ok's as there are lines printed will thus be created. The technically cleanest way to do things would be with |
However, as every expression in Erlang has to return something, printing text returns 'ok'. A list with as many 'ok's as there are lines printed will thus be created. The technically cleanest way to do things would be with |
||
<tt>lists:foreach/2</tt>, which also guarantees evaluation |
<tt>lists:foreach/2</tt>, which also guarantees evaluation |
||
order: |
order: |
||
< |
<syntaxhighlight lang="erlang">lists:foreach(fun({A,B,C}) -> |
||
io:format("~s~n",[[A,B,C]]) end, |
io:format("~s~n",[[A,B,C]]) end, |
||
lists:zip3("abc", "ABC", "123")).</ |
lists:zip3("abc", "ABC", "123")).</syntaxhighlight> |
||
If the lists are not all the same length, an error is thrown. |
If the lists are not all the same length, an error is thrown. |
||
Line 1,425: | Line 1,425: | ||
are. |
are. |
||
If they are all "strings", it's quite easy: |
If they are all "strings", it's quite easy: |
||
<syntaxhighlight lang="euphoria"> |
|||
<lang Euphoria> |
|||
sequence a, b, c |
sequence a, b, c |
||
Line 1,435: | Line 1,435: | ||
puts(1, a[i] & b[i] & c[i] & "\n") |
puts(1, a[i] & b[i] & c[i] & "\n") |
||
end for |
end for |
||
</syntaxhighlight> |
|||
</lang> |
|||
If not, and the other sequence is known to contain only integers: |
If not, and the other sequence is known to contain only integers: |
||
<syntaxhighlight lang="euphoria"> |
|||
<lang Euphoria> |
|||
sequence a, b, c |
sequence a, b, c |
||
Line 1,449: | Line 1,449: | ||
printf(1, "%s%s%g\n", {a[i], b[i], c[i]}) |
printf(1, "%s%s%g\n", {a[i], b[i], c[i]}) |
||
end for |
end for |
||
</syntaxhighlight> |
|||
</lang> |
|||
A general solution for any arbitrary strings of characters or numbers |
A general solution for any arbitrary strings of characters or numbers |
||
Line 1,455: | Line 1,455: | ||
printed out. One possible answer is as follows, if you know that only |
printed out. One possible answer is as follows, if you know that only |
||
alphanumeric characters are used: |
alphanumeric characters are used: |
||
<syntaxhighlight lang="euphoria"> |
|||
<lang Euphoria> |
|||
for i = 1 to length(a) do |
for i = 1 to length(a) do |
||
if (a[i] >= '0' and a[i] <= '9') then |
if (a[i] >= '0' and a[i] <= '9') then |
||
Line 1,468: | Line 1,468: | ||
printf(1, "%s%s%s\n", {a[i], b[i], c[i]}) |
printf(1, "%s%s%s\n", {a[i], b[i], c[i]}) |
||
end for |
end for |
||
</syntaxhighlight> |
|||
</lang> |
|||
Just as in Java, using single quotes around a character gives you its |
Just as in Java, using single quotes around a character gives you its |
||
"char value". In Euphoria, though, it is simply that character's code |
"char value". In Euphoria, though, it is simply that character's code |
||
Line 1,477: | Line 1,477: | ||
=={{header|F_Sharp|F#}}== |
=={{header|F_Sharp|F#}}== |
||
< |
<syntaxhighlight lang="fsharp">for c1,c2,n in Seq.zip3 ['a';'b';'c'] ['A';'B';'C'] |
||
[1;2;3] do |
[1;2;3] do |
||
printfn "%c%c%d" c1 c2 n</ |
printfn "%c%c%d" c1 c2 n</syntaxhighlight> |
||
When one sequence is exhausted, any remaining elements in the other |
When one sequence is exhausted, any remaining elements in the other |
||
Line 1,485: | Line 1,485: | ||
=={{header|Factor}}== |
=={{header|Factor}}== |
||
< |
<syntaxhighlight lang="factor">"abc" "ABC" "123" [ [ write1 ] tri@ nl ] |
||
3each</ |
3each</syntaxhighlight> |
||
=={{header|Fantom}}== |
=={{header|Fantom}}== |
||
This will stop when it reaches the end of the shortest list. |
This will stop when it reaches the end of the shortest list. |
||
< |
<syntaxhighlight lang="fantom"> |
||
class LoopMultiple |
class LoopMultiple |
||
{ |
{ |
||
Line 1,505: | Line 1,505: | ||
} |
} |
||
} |
} |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Fermat}}== |
=={{header|Fermat}}== |
||
< |
<syntaxhighlight lang="fermat">[a] := [('a','b','c')]; |
||
[b] := [('A','B','C')]; |
[b] := [('A','B','C')]; |
||
[c] := [(1,2,3)]; |
[c] := [(1,2,3)]; |
||
Line 1,516: | Line 1,516: | ||
;{instead of a numerical ASCII code, and the} |
;{instead of a numerical ASCII code, and the} |
||
;{:1 causes the integer to take up exactly one} |
;{:1 causes the integer to take up exactly one} |
||
;{space, ie. no leading or trailing spaces.}</ |
;{space, ie. no leading or trailing spaces.}</syntaxhighlight> |
||
{{out}}</pre>aA1 |
{{out}}</pre>aA1 |
||
bB2 |
bB2 |
||
Line 1,522: | Line 1,522: | ||
=={{header|Forth}}== |
=={{header|Forth}}== |
||
< |
<syntaxhighlight lang="forth">create a char a , char b , char c , |
||
create b char A , char B , char C , |
create b char A , char B , char C , |
||
create c char 1 , char 2 , char 3 , |
create c char 1 , char 2 , char 3 , |
||
Line 1,540: | Line 1,540: | ||
loop |
loop |
||
drop drop drop |
drop drop drop |
||
;</ |
;</syntaxhighlight> |
||
=={{header|Fortran}}== |
=={{header|Fortran}}== |
||
< |
<syntaxhighlight lang="fortran">program main |
||
implicit none |
implicit none |
||
Line 1,559: | Line 1,559: | ||
end program main |
end program main |
||
</syntaxhighlight> |
|||
</lang> |
|||
If the arrays are of different length (say, array ns has no third element), then when its turn comes the next unit of storage along from the second element will be accessed, its content interpreted as an integer, and its decimal value printed... If however, array bound checking is activated (or there is a memory access protection scheme that would detect this), a feature unavailable via many compilers and not the default on the rest, then an error will be detected and the run will be terminated, possibly with a somewhat helpful message. |
If the arrays are of different length (say, array ns has no third element), then when its turn comes the next unit of storage along from the second element will be accessed, its content interpreted as an integer, and its decimal value printed... If however, array bound checking is activated (or there is a memory access protection scheme that would detect this), a feature unavailable via many compilers and not the default on the rest, then an error will be detected and the run will be terminated, possibly with a somewhat helpful message. |
||
Line 1,566: | Line 1,566: | ||
=={{header|FreeBASIC}}== |
=={{header|FreeBASIC}}== |
||
< |
<syntaxhighlight lang="freebasic">' FB 1.05.0 Win64 |
||
Function min(x As Integer, y As Integer) As Integer |
Function min(x As Integer, y As Integer) As Integer |
||
Line 1,594: | Line 1,594: | ||
Print |
Print |
||
Sleep</ |
Sleep</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 1,607: | Line 1,607: | ||
=={{header|Frink}}== |
=={{header|Frink}}== |
||
< |
<syntaxhighlight lang="frink">a1 = ["a", "b", "c"] |
||
a2 = ["A", "B", "C"] |
a2 = ["A", "B", "C"] |
||
a3 = ["1", "2", "3"] |
a3 = ["1", "2", "3"] |
||
m = [a1, a2, a3] |
m = [a1, a2, a3] |
||
for row = m.transpose[] |
for row = m.transpose[] |
||
println[join["",row]]</ |
println[join["",row]]</syntaxhighlight> |
||
=={{header|FunL}}== |
=={{header|FunL}}== |
||
< |
<syntaxhighlight lang="funl">import lists.zip3 |
||
for x <- zip3( ['a', 'b', 'c'], ['A', 'B', 'C'], [1, 2, 3] ) |
for x <- zip3( ['a', 'b', 'c'], ['A', 'B', 'C'], [1, 2, 3] ) |
||
println( x.mkString() )</ |
println( x.mkString() )</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 1,630: | Line 1,630: | ||
=={{header|Gambas}}== |
=={{header|Gambas}}== |
||
'''[https://gambas-playground.proko.eu/?gist=3a69e733694aeab3b72c6a5c0316535b Click this link to run this code]''' |
'''[https://gambas-playground.proko.eu/?gist=3a69e733694aeab3b72c6a5c0316535b Click this link to run this code]''' |
||
< |
<syntaxhighlight lang="gambas">Public Sub Main() |
||
Dim a1 As String[] = ["a", "b", "c"] |
Dim a1 As String[] = ["a", "b", "c"] |
||
Dim a2 As String[] = ["A", "B", "C"] |
Dim a2 As String[] = ["A", "B", "C"] |
||
Line 1,640: | Line 1,640: | ||
Next |
Next |
||
End</ |
End</syntaxhighlight> |
||
Output: |
Output: |
||
Line 1,650: | Line 1,650: | ||
=={{header|GAP}}== |
=={{header|GAP}}== |
||
< |
<syntaxhighlight lang="gap"> |
||
# The Loop function will apply some function to every tuple built by |
# The Loop function will apply some function to every tuple built by |
||
taking |
taking |
||
Line 1,685: | Line 1,685: | ||
aC3 |
aC3 |
||
bD1 |
bD1 |
||
aE2</ |
aE2</syntaxhighlight> |
||
=={{header|Go}}== |
=={{header|Go}}== |
||
Line 1,700: | Line 1,700: | ||
conditions are meaningful in your application and explicitly handle |
conditions are meaningful in your application and explicitly handle |
||
whatever errors are plausible. |
whatever errors are plausible. |
||
< |
<syntaxhighlight lang="go">package main |
||
import "fmt" |
import "fmt" |
||
Line 1,712: | Line 1,712: | ||
fmt.Printf("%v%c%v\n", a1[i], a2[i], a3[i]) |
fmt.Printf("%v%c%v\n", a1[i], a2[i], a3[i]) |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
=={{header|Golfscript}}== |
=={{header|Golfscript}}== |
||
< |
<syntaxhighlight lang="golfscript">["a" "b" "c"]:a; |
||
["A" "B" "C"]:b; |
["A" "B" "C"]:b; |
||
["1" "2" "3"]:c; |
["1" "2" "3"]:c; |
||
[a b c]zip{puts}/</ |
[a b c]zip{puts}/</syntaxhighlight> |
||
If there are arrays of different size, the shorter are treated as |
If there are arrays of different size, the shorter are treated as |
||
Line 1,725: | Line 1,725: | ||
=={{header|Groovy}}== |
=={{header|Groovy}}== |
||
Solution: |
Solution: |
||
< |
<syntaxhighlight lang="groovy">def synchedConcat = { a1, a2, a3 -> |
||
assert a1 && a2 && a3 |
assert a1 && a2 && a3 |
||
assert a1.size() == a2.size() |
assert a1.size() == a2.size() |
||
assert a2.size() == a3.size() |
assert a2.size() == a3.size() |
||
[a1, a2, a3].transpose().collect { "${it[0]}${it[1]}${it[2]}" } |
[a1, a2, a3].transpose().collect { "${it[0]}${it[1]}${it[2]}" } |
||
}</ |
}</syntaxhighlight> |
||
Test: |
Test: |
||
< |
<syntaxhighlight lang="groovy">def x = ['a', 'b', 'c'] |
||
def y = ['A', 'B', 'C'] |
def y = ['A', 'B', 'C'] |
||
def z = [1, 2, 3] |
def z = [1, 2, 3] |
||
synchedConcat(x, y, z).each { println it }</ |
synchedConcat(x, y, z).each { println it }</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 1,746: | Line 1,746: | ||
=={{header|Harbour}}== |
=={{header|Harbour}}== |
||
'''Using FOR EACH ... NEXT statement''' |
'''Using FOR EACH ... NEXT statement''' |
||
< |
<syntaxhighlight lang="visualfoxpro"> |
||
PROCEDURE Main() |
PROCEDURE Main() |
||
LOCAL a1 := { "a", "b", "c" }, ; |
LOCAL a1 := { "a", "b", "c" }, ; |
||
Line 1,757: | Line 1,757: | ||
NEXT |
NEXT |
||
RETURN |
RETURN |
||
</syntaxhighlight> |
|||
</lang> |
|||
Output: |
Output: |
||
aA1 |
aA1 |
||
Line 1,768: | Line 1,768: | ||
'''Using list comprehension''' |
'''Using list comprehension''' |
||
< |
<syntaxhighlight lang="haskell">{-# LANGUAGE ParallelListComp #-} |
||
main = sequence [ putStrLn [x, y, z] | x <- "abd" | y <- "ABC" | z <- "123"]</ |
main = sequence [ putStrLn [x, y, z] | x <- "abd" | y <- "ABC" | z <- "123"]</syntaxhighlight> |
||
'''Using Transpose''' |
'''Using Transpose''' |
||
Line 1,775: | Line 1,775: | ||
In this special case of transposing strings. |
In this special case of transposing strings. |
||
< |
<syntaxhighlight lang="haskell">import Data.List |
||
main = mapM putStrLn $ transpose ["abd", "ABC", "123"]</ |
main = mapM putStrLn $ transpose ["abd", "ABC", "123"]</syntaxhighlight> |
||
'''Using ZipWith*''' |
'''Using ZipWith*''' |
||
< |
<syntaxhighlight lang="haskell">import Data.List |
||
main = mapM putStrLn $ zipWith3 (\a b c -> [a,b,c]) "abc" "ABC" "123"</ |
main = mapM putStrLn $ zipWith3 (\a b c -> [a,b,c]) "abc" "ABC" "123"</syntaxhighlight> |
||
'''Using applicative ZipLists''' |
'''Using applicative ZipLists''' |
||
ZipLists generalize zipWith to any number of parameters |
ZipLists generalize zipWith to any number of parameters |
||
< |
<syntaxhighlight lang="haskell">import Control.Applicative (ZipList (ZipList, getZipList)) |
||
main :: IO [()] |
main :: IO [()] |
||
Line 1,795: | Line 1,795: | ||
<$> ZipList "abd" |
<$> ZipList "abd" |
||
<*> ZipList "ABC" |
<*> ZipList "ABC" |
||
<*> ZipList "123"</ |
<*> ZipList "123"</syntaxhighlight> |
||
=={{header|Haxe}}== |
=={{header|Haxe}}== |
||
< |
<syntaxhighlight lang="haxe">using Lambda; |
||
using Std; |
using Std; |
||
Line 1,819: | Line 1,819: | ||
Sys.println(a[i] + b[i] + c[i].string()); |
Sys.println(a[i] + b[i] + c[i].string()); |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
=={{header|HicEst}}== |
=={{header|HicEst}}== |
||
< |
<syntaxhighlight lang="hicest">CHARACTER :: A = "abc" |
||
REAL :: C(3) |
REAL :: C(3) |
||
Line 1,829: | Line 1,829: | ||
DO i = 1, 3 |
DO i = 1, 3 |
||
WRITE() A(i), "ABC"(i), C(i) |
WRITE() A(i), "ABC"(i), C(i) |
||
ENDDO</ |
ENDDO</syntaxhighlight> |
||
=={{header|Icon}} and {{header|Unicon}}== |
=={{header|Icon}} and {{header|Unicon}}== |
||
The first solution uses co-expressions to produce parallel evaluation. |
The first solution uses co-expressions to produce parallel evaluation. |
||
< |
<syntaxhighlight lang="icon">procedure main() |
||
a := create !["a","b","c"] |
a := create !["a","b","c"] |
||
b := create !["A","B","C"] |
b := create !["A","B","C"] |
||
c := create !["1","2","3"] |
c := create !["1","2","3"] |
||
while write(@a,@b,@c) |
while write(@a,@b,@c) |
||
end</ |
end</syntaxhighlight> |
||
The second solution is more like other procedural languages |
The second solution is more like other procedural languages |
||
and also handles unequal list lengths. |
and also handles unequal list lengths. |
||
< |
<syntaxhighlight lang="icon">link numbers # for max |
||
procedure main() |
procedure main() |
||
Line 1,853: | Line 1,853: | ||
every i := 1 to max(*a,*b,*c) do |
every i := 1 to max(*a,*b,*c) do |
||
write(a[i]|"","\t",b[i]|"","\t",c[i]|"") |
write(a[i]|"","\t",b[i]|"","\t",c[i]|"") |
||
end</ |
end</syntaxhighlight> |
||
{{libheader|Icon Programming Library}} |
{{libheader|Icon Programming Library}} |
||
Line 1,861: | Line 1,861: | ||
=={{header|J}}== |
=={{header|J}}== |
||
For arrays of different types: |
For arrays of different types: |
||
< |
<syntaxhighlight lang="j"> ,.&:(":"0@>)/ 'abc' ; 'ABC' ; 1 2 3 |
||
aA1 |
aA1 |
||
bB2 |
bB2 |
||
cC3</ |
cC3</syntaxhighlight> |
||
This approach works by representing the digits as characters. |
This approach works by representing the digits as characters. |
||
Where arrays are all the same type (all numeric or all string): |
Where arrays are all the same type (all numeric or all string): |
||
< |
<syntaxhighlight lang="j"> ,.&:>/ 'abc' ; 'ABC' ; '123' |
||
aA1 |
aA1 |
||
bB2 |
bB2 |
||
cC3</ |
cC3</syntaxhighlight> |
||
Both of these implementations reject arrays with conflicting lengths. |
Both of these implementations reject arrays with conflicting lengths. |
||
Line 1,877: | Line 1,877: | ||
Other options include: |
Other options include: |
||
< |
<syntaxhighlight lang="j"> |: 'abc', 'ABC' ,:;":&> 1 2 3 |
||
aA1 |
aA1 |
||
bB2 |
bB2 |
||
cC3</ |
cC3</syntaxhighlight> |
||
< |
<syntaxhighlight lang="j"> |: 'abc', 'ABC',: '123' |
||
aA1 |
aA1 |
||
bB2 |
bB2 |
||
cC3</ |
cC3</syntaxhighlight> |
||
These implementations pad short arrays with spaces. |
These implementations pad short arrays with spaces. |
||
Or: |
Or: |
||
< |
<syntaxhighlight lang="j"> |:>]&.>L:_1 'abc';'ABC';<1 2 3 |
||
┌─┬─┬─┐ |
┌─┬─┬─┐ |
||
│a│A│1│ |
│a│A│1│ |
||
Line 1,896: | Line 1,896: | ||
├─┼─┼─┤ |
├─┼─┼─┤ |
||
│c│C│3│ |
│c│C│3│ |
||
└─┴─┴─┘</ |
└─┴─┴─┘</syntaxhighlight> |
||
This implementation puts each item from each of the original lists |
This implementation puts each item from each of the original lists |
||
Line 1,910: | Line 1,910: | ||
=={{header|Java}}== |
=={{header|Java}}== |
||
< |
<syntaxhighlight lang="java">String[][] list1 = {{"a","b","c"}, {"A", "B", "C"}, {"1", "2", "3"}}; |
||
for (int i = 0; i < list1.length; i++) { |
for (int i = 0; i < list1.length; i++) { |
||
for (String[] lista : list1) { |
for (String[] lista : list1) { |
||
Line 1,916: | Line 1,916: | ||
} |
} |
||
System.out.println(); |
System.out.println(); |
||
}</ |
}</syntaxhighlight> |
||
=={{header|JavaScript}}== |
=={{header|JavaScript}}== |
||
Line 1,923: | Line 1,923: | ||
This loops over the indices of the first array, |
This loops over the indices of the first array, |
||
and uses that to index into the others. |
and uses that to index into the others. |
||
< |
<syntaxhighlight lang="javascript">var a = ["a","b","c"], |
||
b = ["A","B","C"], |
b = ["A","B","C"], |
||
c = [1,2,3], |
c = [1,2,3], |
||
Line 1,930: | Line 1,930: | ||
for (i = 0; i < a.length; i += 1) { |
for (i = 0; i < a.length; i += 1) { |
||
output += a[i] + b[i] + c[i] + "\n"; |
output += a[i] + b[i] + c[i] + "\n"; |
||
}</ |
}</syntaxhighlight> |
||
If the b or c arrays are too "short", |
If the b or c arrays are too "short", |
||
you will see the string "undefined" appear in the output. |
you will see the string "undefined" appear in the output. |
||
Line 1,936: | Line 1,936: | ||
Alternatively, we can nest a couple of calls to '''.forEach()''': one for the array of three arrays, and one for each of the three index positions: |
Alternatively, we can nest a couple of calls to '''.forEach()''': one for the array of three arrays, and one for each of the three index positions: |
||
< |
<syntaxhighlight lang="javascript">var lstOut = ['', '', '']; |
||
[["a", "b", "c"], ["A", "B", "C"], ["1", "2", "3"]].forEach( |
[["a", "b", "c"], ["A", "B", "C"], ["1", "2", "3"]].forEach( |
||
Line 1,949: | Line 1,949: | ||
); |
); |
||
// lstOut --> ["aA1", "bB2", "cC3"]</ |
// lstOut --> ["aA1", "bB2", "cC3"]</syntaxhighlight> |
||
===Functional composition=== |
===Functional composition=== |
||
Line 1,961: | Line 1,961: | ||
Reduce / fold: |
Reduce / fold: |
||
< |
<syntaxhighlight lang="javascript">(function (lstArrays) { |
||
return lstArrays.reduce( |
return lstArrays.reduce( |
||
Line 1,977: | Line 1,977: | ||
["A", "B", "C"], |
["A", "B", "C"], |
||
["1", "2", "3"] |
["1", "2", "3"] |
||
]);</ |
]);</syntaxhighlight> |
||
A fixed arity ZipWith: |
A fixed arity ZipWith: |
||
< |
<syntaxhighlight lang="javascript">(function (x, y, z) { |
||
// function of arity 3 mapped over nth items of each of 3 lists |
// function of arity 3 mapped over nth items of each of 3 lists |
||
Line 1,996: | Line 1,996: | ||
return zipWith3(concat, x, y, z).join('\n') |
return zipWith3(concat, x, y, z).join('\n') |
||
})(["a", "b", "c"], ["A", "B", "C"], [1, 2, 3]);</ |
})(["a", "b", "c"], ["A", "B", "C"], [1, 2, 3]);</syntaxhighlight> |
||
Or we could write a generic '''zipListsWith''' applying some supplied function overs lists derived from the nth members of an arbitrary list of (equal-length) lists. |
Or we could write a generic '''zipListsWith''' applying some supplied function overs lists derived from the nth members of an arbitrary list of (equal-length) lists. |
||
< |
<syntaxhighlight lang="javascript">(function () { |
||
'use strict'; |
'use strict'; |
||
Line 2,024: | Line 2,024: | ||
) |
) |
||
.join('\n'); |
.join('\n'); |
||
})();</ |
})();</syntaxhighlight> |
||
{{Out}} |
{{Out}} |
||
<syntaxhighlight lang="javascript">aA1 |
|||
<lang JavaScript>aA1 |
|||
bB2 |
bB2 |
||
cC3</ |
cC3</syntaxhighlight> |
||
====ES6==== |
====ES6==== |
||
By transposition: |
By transposition: |
||
< |
<syntaxhighlight lang="javascript">(() => { |
||
'use strict'; |
'use strict'; |
||
Line 2,065: | Line 2,065: | ||
map(concat, transpose(xs)) |
map(concat, transpose(xs)) |
||
); |
); |
||
})();</ |
})();</syntaxhighlight> |
||
{{Out}} |
{{Out}} |
||
<pre>aA1 |
<pre>aA1 |
||
Line 2,079: | Line 2,079: | ||
The first array determines the number of items in the output; |
The first array determines the number of items in the output; |
||
nulls are used for padding. |
nulls are used for padding. |
||
< |
<syntaxhighlight lang="jq"># zip/0 emits [] if input is []. |
||
def zip: |
def zip: |
||
. as $in |
. as $in |
||
| [range(0; $in[0]|length) as $i | $in | map( .[$i] ) ];</ |
| [range(0; $in[0]|length) as $i | $in | map( .[$i] ) ];</syntaxhighlight> |
||
Example 1: |
Example 1: |
||
Line 2,120: | Line 2,120: | ||
that pads all arrays shorter than the longest with nulls. |
that pads all arrays shorter than the longest with nulls. |
||
Here is such a variant: |
Here is such a variant: |
||
<syntaxhighlight lang="jq"> |
|||
<lang jq> |
|||
# transpose a possibly jagged matrix |
# transpose a possibly jagged matrix |
||
def transpose: |
def transpose: |
||
Line 2,129: | Line 2,129: | ||
([]; . + [ [ $row[$i] ] + $t[$i] ]) |
([]; . + [ [ $row[$i] ] + $t[$i] ]) |
||
end; |
end; |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Jsish}}== |
=={{header|Jsish}}== |
||
< |
<syntaxhighlight lang="javascript">/* Loop over multiple arrays, in Jsish */ |
||
var a1 = ['a', 'b', 'c']; |
var a1 = ['a', 'b', 'c']; |
||
var a2 = ['A', 'B', 'C']; |
var a2 = ['A', 'B', 'C']; |
||
Line 2,168: | Line 2,168: | ||
undefinedundefinedundefinedundefined7 |
undefinedundefinedundefinedundefined7 |
||
=!EXPECTEND!= |
=!EXPECTEND!= |
||
*/</ |
*/</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 2,176: | Line 2,176: | ||
=={{header|Julia}}== |
=={{header|Julia}}== |
||
'''With a higher order function''': |
'''With a higher order function''': |
||
< |
<syntaxhighlight lang="julia">foreach(println, ('a', 'b', 'c'), ('A', 'B', 'C'), (1, 2, 3))</syntaxhighlight> |
||
'''With a loop''': |
'''With a loop''': |
||
< |
<syntaxhighlight lang="julia">for (i, j, k) in zip(('a', 'b', 'c'), ('A', 'B', 'C'), (1, 2, 3)) |
||
println(i, j, k) |
println(i, j, k) |
||
end</ |
end</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 2,189: | Line 2,189: | ||
=={{header|K}}== |
=={{header|K}}== |
||
< |
<syntaxhighlight lang="k">{,/$x}'+("abc";"ABC";1 2 3)</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 2,202: | Line 2,202: | ||
The following is a more general approach where |
The following is a more general approach where |
||
<syntaxhighlight lang="k"> |
|||
<lang K> |
|||
&/#:'x |
&/#:'x |
||
</syntaxhighlight> |
|||
</lang> |
|||
calculates the minimum length of the arrays |
calculates the minimum length of the arrays |
||
and is used to index the first elements in each array. |
and is used to index the first elements in each array. |
||
<syntaxhighlight lang="k"> |
|||
<lang K> |
|||
{+x[;!(&/#:'x)]}("abc";"ABC";"1234") |
{+x[;!(&/#:'x)]}("abc";"ABC";"1234") |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
Line 2,223: | Line 2,223: | ||
then the arrays must be converted to strings. |
then the arrays must be converted to strings. |
||
<syntaxhighlight lang="k"> |
|||
<lang K> |
|||
{a:,/'($:'x);+a[;!(&/#:'a)]}("abc";"ABC";1 2 3 4) |
{a:,/'($:'x);+a[;!(&/#:'a)]}("abc";"ABC";1 2 3 4) |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Kotlin}}== |
=={{header|Kotlin}}== |
||
< |
<syntaxhighlight lang="scala">// version 1.0.6 |
||
fun main(args: Array<String>) { |
fun main(args: Array<String>) { |
||
Line 2,242: | Line 2,242: | ||
val minSize = Math.min(a2.size, Math.min(a4.size, a5.size)) // minimum size of a2, a4 and a5 |
val minSize = Math.min(a2.size, Math.min(a4.size, a5.size)) // minimum size of a2, a4 and a5 |
||
for(i in 0 until minSize) println("${a2[i]}${a4[i]}${a5[i]}") |
for(i in 0 until minSize) println("${a2[i]}${a4[i]}${a5[i]}") |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 2,255: | Line 2,255: | ||
=={{header|Lambdatalk}}== |
=={{header|Lambdatalk}}== |
||
< |
<syntaxhighlight lang="scheme"> |
||
1) loop over 3 sentences of equal length and returning 3 sentences |
1) loop over 3 sentences of equal length and returning 3 sentences |
||
Line 2,308: | Line 2,308: | ||
e d |
e d |
||
s |
s |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|LFE}}== |
=={{header|LFE}}== |
||
< |
<syntaxhighlight lang="lisp"> |
||
(lists:zipwith3 |
(lists:zipwith3 |
||
(lambda (i j k) |
(lambda (i j k) |
||
Line 2,318: | Line 2,318: | ||
'(A B C) |
'(A B C) |
||
'(1 2 3)) |
'(1 2 3)) |
||
</syntaxhighlight> |
|||
</lang> |
|||
If any of the data lists differ in size from the other, |
If any of the data lists differ in size from the other, |
||
Line 2,332: | Line 2,332: | ||
=={{header|Liberty BASIC}}== |
=={{header|Liberty BASIC}}== |
||
< |
<syntaxhighlight lang="lb">a$(1)="a" : a$(2)="b" : a$(3)="c" |
||
b$(1)="A" : b$(2)="B" : b$(3)="C" |
b$(1)="A" : b$(2)="B" : b$(3)="C" |
||
c(1)=1 : c(2)=2 : c(3)=3 |
c(1)=1 : c(2)=2 : c(3)=3 |
||
Line 2,339: | Line 2,339: | ||
for i = 1 to 3 |
for i = 1 to 3 |
||
print a$(i);b$(i);c(i) |
print a$(i);b$(i);c(i) |
||
next</ |
next</syntaxhighlight> |
||
=={{header|Lisaac}}== |
=={{header|Lisaac}}== |
||
< |
<syntaxhighlight lang="lisaac">Section Header |
||
+ name := ARRAY_LOOP_TEST; |
+ name := ARRAY_LOOP_TEST; |
||
Line 2,368: | Line 2,368: | ||
'\n'.print; |
'\n'.print; |
||
}; |
}; |
||
);</ |
);</syntaxhighlight> |
||
=={{header|LiveCode}}== |
=={{header|LiveCode}}== |
||
Arrays |
Arrays |
||
< |
<syntaxhighlight lang="livecode">command loopArrays |
||
local lowA, uppA, nums, z |
local lowA, uppA, nums, z |
||
put "a,b,c" into lowA |
put "a,b,c" into lowA |
||
Line 2,387: | Line 2,387: | ||
put z |
put z |
||
end loopArrays</ |
end loopArrays</syntaxhighlight> |
||
"list" processing |
"list" processing |
||
< |
<syntaxhighlight lang="livecode">command loopDelimitedList |
||
local lowA, uppA, nums, z |
local lowA, uppA, nums, z |
||
put "a,b,c" into lowA |
put "a,b,c" into lowA |
||
Line 2,401: | Line 2,401: | ||
put z |
put z |
||
end loopDelimitedList</ |
end loopDelimitedList</syntaxhighlight> |
||
Output - both behave similarly for this exercise. |
Output - both behave similarly for this exercise. |
||
<pre>aA1 |
<pre>aA1 |
||
Line 2,418: | Line 2,418: | ||
{{works with|UCB Logo}} |
{{works with|UCB Logo}} |
||
< |
<syntaxhighlight lang="logo">show (map [(word ?1 ?2 ?3)] [a b c] [A B C] [1 2 3]) |
||
; [aA1 bB2 cC3] |
; [aA1 bB2 cC3] |
||
(foreach [a b c] [A B C] [1 2 3] [print (word ?1 ?2 ?3)]) ; as above, |
(foreach [a b c] [A B C] [1 2 3] [print (word ?1 ?2 ?3)]) ; as above, |
||
one per line</ |
one per line</syntaxhighlight> |
||
=={{header|Lua}}== |
=={{header|Lua}}== |
||
This can be done with a simple for loop: |
This can be done with a simple for loop: |
||
< |
<syntaxhighlight lang="lua"> |
||
a1, a2, a3 = {'a' , 'b' , 'c' } , { 'A' , 'B' , 'C' } , { 1 , 2 , 3 } |
a1, a2, a3 = {'a' , 'b' , 'c' } , { 'A' , 'B' , 'C' } , { 1 , 2 , 3 } |
||
for i = 1, 3 do print(a1[i]..a2[i]..a3[i]) end |
for i = 1, 3 do print(a1[i]..a2[i]..a3[i]) end |
||
</syntaxhighlight> |
|||
</lang> |
|||
but it may be more enlightening |
but it may be more enlightening |
||
(and in line with the spirit of the challenge) to use the generic for: |
(and in line with the spirit of the challenge) to use the generic for: |
||
< |
<syntaxhighlight lang="lua"> |
||
function iter(a, b, c) |
function iter(a, b, c) |
||
local i = 0 |
local i = 0 |
||
Line 2,442: | Line 2,442: | ||
for u, v, w in iter(a1, a2, a3) do print(u..v..w) end |
for u, v, w in iter(a1, a2, a3) do print(u..v..w) end |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Maple}}== |
=={{header|Maple}}== |
||
< |
<syntaxhighlight lang="maple"># Set up |
||
L := [["a", "b", "c"],["A", "B", "C"], ["1", "2", "3"]]; |
L := [["a", "b", "c"],["A", "B", "C"], ["1", "2", "3"]]; |
||
M := Array(1..3, 1..3, L); |
M := Array(1..3, 1..3, L); |
||
Line 2,459: | Line 2,459: | ||
end proc: |
end proc: |
||
multi_loop(M);</ |
multi_loop(M);</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 2,469: | Line 2,469: | ||
=={{header|Mathematica}}/{{header|Wolfram Language}}== |
=={{header|Mathematica}}/{{header|Wolfram Language}}== |
||
This can be done with a built-in function: |
This can be done with a built-in function: |
||
< |
<syntaxhighlight lang="mathematica">MapThread[Print, {{"a", "b", "c"}, {"A", "B", "C"}, {1, 2, 3}}];</syntaxhighlight> |
||
All arguments must be lists of the same length. |
All arguments must be lists of the same length. |
||
=={{header|Mercury}}== |
=={{header|Mercury}}== |
||
<lang> |
<syntaxhighlight lang="text"> |
||
:- module multi_array_loop. |
:- module multi_array_loop. |
||
:- interface. |
:- interface. |
||
Line 2,493: | Line 2,493: | ||
print_elems(A, B, C, !IO) :- |
print_elems(A, B, C, !IO) :- |
||
io.format("%c%c%i\n", [c(A), c(B), i(C)], !IO). |
io.format("%c%c%i\n", [c(A), c(B), i(C)], !IO). |
||
</syntaxhighlight> |
|||
</lang> |
|||
The foldl_corresponding family of procedures all throw a |
The foldl_corresponding family of procedures all throw a |
||
software_error/1 |
software_error/1 |
||
Line 2,499: | Line 2,499: | ||
=={{header|Modula-3}}== |
=={{header|Modula-3}}== |
||
< |
<syntaxhighlight lang="modula3">MODULE MultiArray EXPORTS Main; |
||
IMPORT IO, Fmt; |
IMPORT IO, Fmt; |
||
Line 2,515: | Line 2,515: | ||
Fmt.Int(arr3[i]) & "\n"); |
Fmt.Int(arr3[i]) & "\n"); |
||
END; |
END; |
||
END MultiArray.</ |
END MultiArray.</syntaxhighlight> |
||
=={{header|MUMPS}}== |
=={{header|MUMPS}}== |
||
Pieces of String version |
Pieces of String version |
||
<syntaxhighlight lang="mumps"> |
|||
<lang MUMPS> |
|||
LOOPMULT |
LOOPMULT |
||
N A,B,C,D,% |
N A,B,C,D,% |
||
Line 2,529: | Line 2,529: | ||
K A,B,C,D,% |
K A,B,C,D,% |
||
Q |
Q |
||
</syntaxhighlight> |
|||
</lang> |
|||
When there aren't enough elements, |
When there aren't enough elements, |
||
a null string will be returned from the $Piece function. |
a null string will be returned from the $Piece function. |
||
Line 2,542: | Line 2,542: | ||
Local arrays version |
Local arrays version |
||
<syntaxhighlight lang="mumps"> |
|||
<lang MUMPS> |
|||
LOOPMULU |
LOOPMULU |
||
N A,B,C,D,% |
N A,B,C,D,% |
||
Line 2,552: | Line 2,552: | ||
S %=$O(A("")) F Q:%="" W !,$G(A(%)),$G(B(%)),$G(C(%)) S %=$O(A(%)) |
S %=$O(A("")) F Q:%="" W !,$G(A(%)),$G(B(%)),$G(C(%)) S %=$O(A(%)) |
||
K A,B,C,D,% |
K A,B,C,D,% |
||
</syntaxhighlight> |
|||
</lang> |
|||
The commented out line will throw an <UNDEFINED> error when trying |
The commented out line will throw an <UNDEFINED> error when trying |
||
Line 2,578: | Line 2,578: | ||
=={{header|Nanoquery}}== |
=={{header|Nanoquery}}== |
||
{{trans|Java}} |
{{trans|Java}} |
||
< |
<syntaxhighlight lang="nanoquery">list1 = {{"a","b","c"}, {"A","B","C"}, {"1","2","3"}} |
||
for i in range(0, len(list1) - 1) |
for i in range(0, len(list1) - 1) |
||
for lista in list1 |
for lista in list1 |
||
Line 2,584: | Line 2,584: | ||
end for |
end for |
||
println |
println |
||
end for</ |
end for</syntaxhighlight> |
||
=={{header|Nemerle}}== |
=={{header|Nemerle}}== |
||
It "feels" better to use zip() for this, |
It "feels" better to use zip() for this, |
||
unfortunately the built in zip() only takes two lists. |
unfortunately the built in zip() only takes two lists. |
||
< |
<syntaxhighlight lang="nemerle">using System; |
||
using System.Console; |
using System.Console; |
||
Line 2,613: | Line 2,613: | ||
WriteLine($"$x$y$z"); |
WriteLine($"$x$y$z"); |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
Alternately: {{trans|C#}} |
Alternately: {{trans|C#}} |
||
< |
<syntaxhighlight lang="nemerle">using System.Console; |
||
module LoopMult |
module LoopMult |
||
Line 2,631: | Line 2,631: | ||
WriteLine("{0}{1}{2}", first[i], second[i], third[i]); |
WriteLine("{0}{1}{2}", first[i], second[i], third[i]); |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
=={{header|NetRexx}}== |
=={{header|NetRexx}}== |
||
< |
<syntaxhighlight lang="netrexx">/* NetRexx */ |
||
options replace format comments java crossref savelog symbols nobinary |
options replace format comments java crossref savelog symbols nobinary |
||
Line 2,679: | Line 2,679: | ||
return smp |
return smp |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre style="overflow:scroll"> |
<pre style="overflow:scroll"> |
||
Line 2,695: | Line 2,695: | ||
=={{header|NewLISP}}== |
=={{header|NewLISP}}== |
||
< |
<syntaxhighlight lang="newlisp">(map println '(a b c) '(A B C) '(1 2 |
||
3))</ |
3))</syntaxhighlight> |
||
=={{header|Nim}}== |
=={{header|Nim}}== |
||
< |
<syntaxhighlight lang="nim">let |
||
a = @['a','b','c'] |
a = @['a','b','c'] |
||
b = @["A","B","C"] |
b = @["A","B","C"] |
||
Line 2,705: | Line 2,705: | ||
for i in 0..2: |
for i in 0..2: |
||
echo a[i], b[i], c[i]</ |
echo a[i], b[i], c[i]</syntaxhighlight> |
||
=={{header|NS-HUBASIC}}== |
=={{header|NS-HUBASIC}}== |
||
< |
<syntaxhighlight lang="ns-hubasic">10 DIM A$(3) |
||
20 DIM B$(3) |
20 DIM B$(3) |
||
30 DIM C$(3) |
30 DIM C$(3) |
||
Line 2,722: | Line 2,722: | ||
130 FOR I=1 TO 3 |
130 FOR I=1 TO 3 |
||
140 PRINT A$(I)B$(I)C$(I) |
140 PRINT A$(I)B$(I)C$(I) |
||
150 NEXT</ |
150 NEXT</syntaxhighlight> |
||
=={{header|Oberon-2}}== |
=={{header|Oberon-2}}== |
||
Works with oo2c version 2 |
Works with oo2c version 2 |
||
< |
<syntaxhighlight lang="oberon2"> |
||
MODULE LoopMArrays; |
MODULE LoopMArrays; |
||
IMPORT |
IMPORT |
||
Line 2,751: | Line 2,751: | ||
DoLoop |
DoLoop |
||
END LoopMArrays. |
END LoopMArrays. |
||
</syntaxhighlight> |
|||
</lang> |
|||
Output:<br/> |
Output:<br/> |
||
<pre> |
<pre> |
||
Line 2,760: | Line 2,760: | ||
=={{header|Objeck}}== |
=={{header|Objeck}}== |
||
< |
<syntaxhighlight lang="objeck"> |
||
class MultipleArrayAccess { |
class MultipleArrayAccess { |
||
function : Main(args : String[]) ~ Nil { |
function : Main(args : String[]) ~ Nil { |
||
Line 2,774: | Line 2,774: | ||
} |
} |
||
} |
} |
||
</syntaxhighlight> |
|||
</lang> |
|||
If the arrays are different lengths, |
If the arrays are different lengths, |
||
Line 2,781: | Line 2,781: | ||
=={{header|OCaml}}== |
=={{header|OCaml}}== |
||
an immediate solution: |
an immediate solution: |
||
< |
<syntaxhighlight lang="ocaml">let a1 = [| 'a'; 'b'; 'c' |] |
||
and a2 = [| 'A'; 'B'; 'C' |] |
and a2 = [| 'A'; 'B'; 'C' |] |
||
and a3 = [| '1'; '2'; '3' |] ;; |
and a3 = [| '1'; '2'; '3' |] ;; |
||
Line 2,790: | Line 2,790: | ||
print_char a3.(i); |
print_char a3.(i); |
||
print_newline() |
print_newline() |
||
) a1 ;;</ |
) a1 ;;</syntaxhighlight> |
||
a more generic solution could be to use a function |
a more generic solution could be to use a function |
||
which iterates over a list of arrays: |
which iterates over a list of arrays: |
||
< |
<syntaxhighlight lang="ocaml">let n_arrays_iter ~f = function |
||
| [] -> () |
| [] -> () |
||
| x::xs as al -> |
| x::xs as al -> |
||
Line 2,805: | Line 2,805: | ||
let ai = List.map (fun a -> a.(i)) al in |
let ai = List.map (fun a -> a.(i)) al in |
||
f ai |
f ai |
||
done</ |
done</syntaxhighlight> |
||
this function raises Invalid_argument exception if arrays have different |
this function raises Invalid_argument exception if arrays have different |
||
Line 2,811: | Line 2,811: | ||
and has this signature: |
and has this signature: |
||
< |
<syntaxhighlight lang="ocaml">val n_arrays_iter : f:('a list -> unit) -> 'a |
||
array list -> unit</ |
array list -> unit</syntaxhighlight> |
||
how to use it with arrays a1, a2 and a3 defined before: |
how to use it with arrays a1, a2 and a3 defined before: |
||
< |
<syntaxhighlight lang="ocaml">let () = |
||
n_arrays_iter [a1; a2; a3] ~f:(fun l -> |
n_arrays_iter [a1; a2; a3] ~f:(fun l -> |
||
List.iter print_char l; |
List.iter print_char l; |
||
print_newline()); |
print_newline()); |
||
;;</ |
;;</syntaxhighlight> |
||
=={{header|Oforth}}== |
=={{header|Oforth}}== |
||
Line 2,826: | Line 2,826: | ||
If arrays don't have the same size, zipAll reduces to the minimum size |
If arrays don't have the same size, zipAll reduces to the minimum size |
||
< |
<syntaxhighlight lang="oforth">[ "a", "b", "c" ] [ "A", "B", "C" ] [ 1, 2, 3 ] |
||
zipAll(3) apply(#[ apply(#print) printcr ])</ |
zipAll(3) apply(#[ apply(#print) printcr ])</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 2,837: | Line 2,837: | ||
=={{header|ooRexx}}== |
=={{header|ooRexx}}== |
||
<syntaxhighlight lang="oorexx"> |
|||
<lang ooRexx> |
|||
x = .array~of("a", "b", "c") |
x = .array~of("a", "b", "c") |
||
y = .array~of("A", "B", "C") |
y = .array~of("A", "B", "C") |
||
Line 2,845: | Line 2,845: | ||
say x[i]y[i]z[i] |
say x[i]y[i]z[i] |
||
end |
end |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Oz}}== |
=={{header|Oz}}== |
||
< |
<syntaxhighlight lang="oz">for |
||
I in [a b c] |
I in [a b c] |
||
J in ['A' 'B' 'C'] |
J in ['A' 'B' 'C'] |
||
Line 2,854: | Line 2,854: | ||
do |
do |
||
{System.showInfo I#J#K} |
{System.showInfo I#J#K} |
||
end</ |
end</syntaxhighlight> |
||
The loop will stop when the shortest list is exhausted. |
The loop will stop when the shortest list is exhausted. |
||
Line 2,860: | Line 2,860: | ||
=={{header|PARI/GP}}== |
=={{header|PARI/GP}}== |
||
This version stops when the shortest vector is exhausted. |
This version stops when the shortest vector is exhausted. |
||
< |
<syntaxhighlight lang="parigp">loopMultiple(V)={ |
||
my(l=#V[1]); |
my(l=#V[1]); |
||
for(i=2,#V,l=min(l,#V[i])); |
for(i=2,#V,l=min(l,#V[i])); |
||
Line 2,869: | Line 2,869: | ||
print() |
print() |
||
) |
) |
||
};</ |
};</syntaxhighlight> |
||
This version prints blanks when a vector is exhausted. |
This version prints blanks when a vector is exhausted. |
||
< |
<syntaxhighlight lang="parigp">loopMultiple(V)={ |
||
my(l=0); |
my(l=0); |
||
for(i=1,#V,l=max(l,#V[i])); |
for(i=1,#V,l=max(l,#V[i])); |
||
Line 2,885: | Line 2,885: | ||
print() |
print() |
||
) |
) |
||
};</ |
};</syntaxhighlight> |
||
=={{header|Pascal}}== |
=={{header|Pascal}}== |
||
Line 2,891: | Line 2,891: | ||
=={{header|Perl}}== |
=={{header|Perl}}== |
||
< |
<syntaxhighlight lang="perl">sub zip (&@) |
||
{ |
{ |
||
my $code = shift; |
my $code = shift; |
||
Line 2,903: | Line 2,903: | ||
my @a3 = qw( 1 2 3 ); |
my @a3 = qw( 1 2 3 ); |
||
zip { print @_,"\n" }\(@a1, @a2, @a3);</ |
zip { print @_,"\n" }\(@a1, @a2, @a3);</syntaxhighlight> |
||
This implementation will stop producing items when the shortest array |
This implementation will stop producing items when the shortest array |
||
Line 2,913: | Line 2,913: | ||
If the arguments were not all the same length, attempts to retrieve non-existent elements could trigger a fatal run-time error, were it not for the min(). In print3, fairly obviously, we only extract up to the shortest length. The builtin columnize() routine can perform a similar task: I have provided a space defval and replaced the 3rd array with a string to ensure we get strings back, and extended it to show how columnize uses that default value for missing entries off the end of the first two arrays. |
If the arguments were not all the same length, attempts to retrieve non-existent elements could trigger a fatal run-time error, were it not for the min(). In print3, fairly obviously, we only extract up to the shortest length. The builtin columnize() routine can perform a similar task: I have provided a space defval and replaced the 3rd array with a string to ensure we get strings back, and extended it to show how columnize uses that default value for missing entries off the end of the first two arrays. |
||
<!--< |
<!--<syntaxhighlight lang="phix">--> |
||
<span style="color: #008080;">procedure</span> <span style="color: #000000;">print3</span><span style="color: #0000FF;">(</span><span style="color: #004080;">sequence</span> <span style="color: #000000;">a</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">b</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">c</span><span style="color: #0000FF;">)</span> |
<span style="color: #008080;">procedure</span> <span style="color: #000000;">print3</span><span style="color: #0000FF;">(</span><span style="color: #004080;">sequence</span> <span style="color: #000000;">a</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">b</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">c</span><span style="color: #0000FF;">)</span> |
||
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #7060A8;">min</span><span style="color: #0000FF;">({</span><span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">a</span><span style="color: #0000FF;">),</span><span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">b</span><span style="color: #0000FF;">),</span><span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">c</span><span style="color: #0000FF;">)})</span> <span style="color: #008080;">do</span> |
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #7060A8;">min</span><span style="color: #0000FF;">({</span><span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">a</span><span style="color: #0000FF;">),</span><span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">b</span><span style="color: #0000FF;">),</span><span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">c</span><span style="color: #0000FF;">)})</span> <span style="color: #008080;">do</span> |
||
Line 2,922: | Line 2,922: | ||
<span style="color: #000000;">print3</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"abc"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"ABC"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">2</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">3</span><span style="color: #0000FF;">})</span> |
<span style="color: #000000;">print3</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"abc"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"ABC"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">2</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">3</span><span style="color: #0000FF;">})</span> |
||
<span style="color: #0000FF;">?</span><span style="color: #7060A8;">columnize</span><span style="color: #0000FF;">({</span><span style="color: #008000;">"abc"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"ABC"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"1234"</span><span style="color: #0000FF;">},{},</span><span style="color: #008000;">' '</span><span style="color: #0000FF;">)</span> |
<span style="color: #0000FF;">?</span><span style="color: #7060A8;">columnize</span><span style="color: #0000FF;">({</span><span style="color: #008000;">"abc"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"ABC"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"1234"</span><span style="color: #0000FF;">},{},</span><span style="color: #008000;">' '</span><span style="color: #0000FF;">)</span> |
||
<!--</ |
<!--</syntaxhighlight>--> |
||
{{out}} |
{{out}} |
||
Line 2,933: | Line 2,933: | ||
=={{header|Phixmonti}}== |
=={{header|Phixmonti}}== |
||
< |
<syntaxhighlight lang="phixmonti">include ..\Utilitys.pmt |
||
( "abc" "ABC" "123" ) |
( "abc" "ABC" "123" ) |
||
Line 2,948: | Line 2,948: | ||
nl |
nl |
||
endfor |
endfor |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|PHP}}== |
=={{header|PHP}}== |
||
< |
<syntaxhighlight lang="php">$a = array('a', 'b', 'c'); |
||
$b = array('A', 'B', 'C'); |
$b = array('A', 'B', 'C'); |
||
$c = array('1', '2', '3'); //These don't *have* to be strings, but it |
$c = array('1', '2', '3'); //These don't *have* to be strings, but it |
||
Line 2,961: | Line 2,961: | ||
foreach ($a as $key => $value){ |
foreach ($a as $key => $value){ |
||
echo "{$a[$key]}{$b[$key]}{$c[$key]}\n"; |
echo "{$a[$key]}{$b[$key]}{$c[$key]}\n"; |
||
}</ |
}</syntaxhighlight> |
||
This implementation throws an exception if the arrays are not all the |
This implementation throws an exception if the arrays are not all the |
||
Line 2,973: | Line 2,973: | ||
If the lists/arrays are of uneven lengths, then the elements in the longer arrays are skipped. |
If the lists/arrays are of uneven lengths, then the elements in the longer arrays are skipped. |
||
< |
<syntaxhighlight lang="picat">import util. |
||
go => |
go => |
||
Line 2,997: | Line 2,997: | ||
println([A,B,C,D].join('')) |
println([A,B,C,D].join('')) |
||
end, |
end, |
||
nl.</ |
nl.</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 3,016: | Line 3,016: | ||
=={{header|PicoLisp}}== |
=={{header|PicoLisp}}== |
||
< |
<syntaxhighlight lang="picolisp">(mapc prinl |
||
'(a b c) |
'(a b c) |
||
'(A B C) |
'(A B C) |
||
(1 2 3) )</ |
(1 2 3) )</syntaxhighlight> |
||
The length of the first argument list controls the operation. |
The length of the first argument list controls the operation. |
||
If subsequent lists are longer, their remaining values are ignored. |
If subsequent lists are longer, their remaining values are ignored. |
||
Line 3,028: | Line 3,028: | ||
avoids the usual off-by-one errors |
avoids the usual off-by-one errors |
||
<syntaxhighlight lang="pike"> |
|||
<lang Pike> |
|||
array a1 = ({ "a", "b", "c" }); |
array a1 = ({ "a", "b", "c" }); |
||
array a2 = ({ "A", "B", "C" }); |
array a2 = ({ "A", "B", "C" }); |
||
Line 3,035: | Line 3,035: | ||
foreach(a1; int index; string char_dummy) |
foreach(a1; int index; string char_dummy) |
||
write("%s%s%s\n", a1[index], a2[index], a3[index]); |
write("%s%s%s\n", a1[index], a2[index], a3[index]); |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{Out}} |
{{Out}} |
||
<pre> |
<pre> |
||
Line 3,044: | Line 3,044: | ||
=={{header|PL/I}}== |
=={{header|PL/I}}== |
||
< |
<syntaxhighlight lang="pli"> |
||
declare P(3) character (1) initial ('a', 'b', 'c'), |
declare P(3) character (1) initial ('a', 'b', 'c'), |
||
Q(3) character (1) initial ('A', 'B', 'C'), |
Q(3) character (1) initial ('A', 'B', 'C'), |
||
Line 3,052: | Line 3,052: | ||
put skip edit (P(i), Q(i), R(i)) (2 A, F(1)); |
put skip edit (P(i), Q(i), R(i)) (2 A, F(1)); |
||
end; |
end; |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|PostScript}}== |
=={{header|PostScript}}== |
||
{{libheader|initlib}} |
{{libheader|initlib}} |
||
< |
<syntaxhighlight lang="postscript"> |
||
% transpose is defined in initlib like this. |
% transpose is defined in initlib like this. |
||
/transpose { |
/transpose { |
||
Line 3,068: | Line 3,068: | ||
% using it. |
% using it. |
||
[[/a /b /c] [/A /B /C] [1 2 3]] transpose |
[[/a /b /c] [/A /B /C] [1 2 3]] transpose |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|PowerBASIC}}== |
=={{header|PowerBASIC}}== |
||
< |
<syntaxhighlight lang="powerbasic">FUNCTION PBMAIN () AS LONG |
||
DIM x(2), y(2) AS STRING * 1 |
DIM x(2), y(2) AS STRING * 1 |
||
DIM z(2) AS LONG |
DIM z(2) AS LONG |
||
Line 3,093: | Line 3,093: | ||
NEXT |
NEXT |
||
CLOSE |
CLOSE |
||
END FUNCTION</ |
END FUNCTION</syntaxhighlight> |
||
=={{header|PowerShell}}== |
=={{header|PowerShell}}== |
||
A cheap and chEasy 'zip' function: |
A cheap and chEasy 'zip' function: |
||
<syntaxhighlight lang="powershell"> |
|||
<lang PowerShell> |
|||
function zip3 ($a1, $a2, $a3) |
function zip3 ($a1, $a2, $a3) |
||
{ |
{ |
||
Line 3,108: | Line 3,108: | ||
} |
} |
||
} |
} |
||
</syntaxhighlight> |
|||
</lang> |
|||
<syntaxhighlight lang="powershell"> |
|||
<lang PowerShell> |
|||
zip3 @('a','b','c') @('A','B','C') @(1,2,3) |
zip3 @('a','b','c') @('A','B','C') @(1,2,3) |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{Out}} |
{{Out}} |
||
<pre> |
<pre> |
||
Line 3,120: | Line 3,120: | ||
c C 3 |
c C 3 |
||
</pre> |
</pre> |
||
<syntaxhighlight lang="powershell"> |
|||
<lang PowerShell> |
|||
zip3 @('a','b','c') @('A','B','C') @(1,2,3) | ForEach-Object {$_.Item1 + $_.Item2 + $_.Item3} |
zip3 @('a','b','c') @('A','B','C') @(1,2,3) | ForEach-Object {$_.Item1 + $_.Item2 + $_.Item3} |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{Out}} |
{{Out}} |
||
<pre> |
<pre> |
||
Line 3,132: | Line 3,132: | ||
=={{header|Prolog}}== |
=={{header|Prolog}}== |
||
Works with SWI-Prolog |
Works with SWI-Prolog |
||
< |
<syntaxhighlight lang="prolog">multiple_arrays(L1, L2, L3) :- |
||
maplist(display, L1, L2, L3). |
maplist(display, L1, L2, L3). |
||
display(A,B,C) :- |
display(A,B,C) :- |
||
writef('%s%s%s\n', [[A],[B],[C]]). |
writef('%s%s%s\n', [[A],[B],[C]]). |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> ?- multiple_arrays("abc", "ABC", "123"). |
<pre> ?- multiple_arrays("abc", "ABC", "123"). |
||
Line 3,152: | Line 3,152: | ||
=={{header|PureBasic}}== |
=={{header|PureBasic}}== |
||
< |
<syntaxhighlight lang="purebasic">OpenConsole() |
||
; Fill arrays |
; Fill arrays |
||
Dim a.s(2) |
Dim a.s(2) |
||
Line 3,166: | Line 3,166: | ||
PrintN(a(Arrayposition) + b(Arrayposition) + Str(c(Arrayposition))) |
PrintN(a(Arrayposition) + b(Arrayposition) + Str(c(Arrayposition))) |
||
Next |
Next |
||
Input() ;wait for Enter before ending</ |
Input() ;wait for Enter before ending</syntaxhighlight> |
||
If they have different lengths there are two cases:<br> |
If they have different lengths there are two cases:<br> |
||
Line 3,182: | Line 3,182: | ||
=={{header|Python}}== |
=={{header|Python}}== |
||
Using <tt>zip()</tt>: |
Using <tt>zip()</tt>: |
||
< |
<syntaxhighlight lang="python">>>> print ( '\n'.join(''.join(x) for x in |
||
zip('abc', 'ABC', '123')) ) |
zip('abc', 'ABC', '123')) ) |
||
aA1 |
aA1 |
||
bB2 |
bB2 |
||
cC3 |
cC3 |
||
>>></ |
>>></syntaxhighlight> |
||
If lists are different lengths, <tt>zip()</tt> stops after |
If lists are different lengths, <tt>zip()</tt> stops after |
||
the shortest one. |
the shortest one. |
||
Using <tt>map()</tt>: |
Using <tt>map()</tt>: |
||
< |
<syntaxhighlight lang="python">>>> print(*map(''.join, zip('abc', 'ABC', '123')), sep='\n') |
||
aA1 |
aA1 |
||
bB2 |
bB2 |
||
cC3 |
cC3 |
||
>>></ |
>>></syntaxhighlight> |
||
If lists are different lengths, <tt>map()</tt> in Python 2.x pretends that the shorter lists were extended with |
If lists are different lengths, <tt>map()</tt> in Python 2.x pretends that the shorter lists were extended with |
||
<tt>None</tt> items; <tt>map()</tt> in Python 3.x stops after the shortest one. |
<tt>None</tt> items; <tt>map()</tt> in Python 3.x stops after the shortest one. |
||
Using <tt>itertools.imap()</tt> (Python 2.x): |
Using <tt>itertools.imap()</tt> (Python 2.x): |
||
< |
<syntaxhighlight lang="python">from itertools import imap |
||
def join3(a,b,c): |
def join3(a,b,c): |
||
print a+b+c |
print a+b+c |
||
imap(join3,'abc','ABC','123')</ |
imap(join3,'abc','ABC','123')</syntaxhighlight> |
||
If lists are differnt lengths, <tt>imap()</tt> stops after |
If lists are differnt lengths, <tt>imap()</tt> stops after |
||
the shortest is exhausted. |
the shortest is exhausted. |
||
Line 3,213: | Line 3,213: | ||
fillvalue argument which defaults to <tt>None</tt> (similar to the behavior of |
fillvalue argument which defaults to <tt>None</tt> (similar to the behavior of |
||
''map()'' in Python 2.x): |
''map()'' in Python 2.x): |
||
< |
<syntaxhighlight lang="python">>>> from itertools import zip_longest |
||
>>> print ( '\n'.join(''.join(x) for x in zip_longest('abc', |
>>> print ( '\n'.join(''.join(x) for x in zip_longest('abc', |
||
'ABCD', '12345', fillvalue='#')) ) |
'ABCD', '12345', fillvalue='#')) ) |
||
Line 3,221: | Line 3,221: | ||
#D4 |
#D4 |
||
##5 |
##5 |
||
>>></ |
>>></syntaxhighlight> |
||
(The Python 2.X equivalent is itertools.izip_longest) |
(The Python 2.X equivalent is itertools.izip_longest) |
||
Line 3,228: | Line 3,228: | ||
The code presented here will loop as many times as the number of characters in the first nest (i.e. "abc" in the example). If either of the other two nests are shorter than the first then the program will report a problem. |
The code presented here will loop as many times as the number of characters in the first nest (i.e. "abc" in the example). If either of the other two nests are shorter than the first then the program will report a problem. |
||
< |
<syntaxhighlight lang="quackery"> [ rot witheach |
||
[ emit |
[ emit |
||
over i^ peek emit |
over i^ peek emit |
||
Line 3,235: | Line 3,235: | ||
2drop ] is task ( $ $ $ --> ) |
2drop ] is task ( $ $ $ --> ) |
||
$ "abc" $ "ABC" $ "123" task</ |
$ "abc" $ "ABC" $ "123" task</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 3,245: | Line 3,245: | ||
=={{header|R}}== |
=={{header|R}}== |
||
< |
<syntaxhighlight lang="r">multiloop <- function(...) |
||
{ |
{ |
||
# Retrieve inputs and convert to a list of character strings |
# Retrieve inputs and convert to a list of character strings |
||
Line 3,265: | Line 3,265: | ||
} |
} |
||
} |
} |
||
multiloop(letters[1:3], LETTERS[1:3], 1:3)</ |
multiloop(letters[1:3], LETTERS[1:3], 1:3)</syntaxhighlight> |
||
Same thing as a single function call. |
Same thing as a single function call. |
||
But throws error if the arrays differ in length. |
But throws error if the arrays differ in length. |
||
<syntaxhighlight lang="r"> |
|||
<lang R> |
|||
apply(data.frame(letters[1:3], LETTERS[1:3], 1:3), 1, |
apply(data.frame(letters[1:3], LETTERS[1:3], 1:3), 1, |
||
function(row) { cat(row, "\n", sep='') }) |
function(row) { cat(row, "\n", sep='') }) |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Racket}}== |
=={{header|Racket}}== |
||
Line 3,280: | Line 3,280: | ||
of sequences of any kind at once: |
of sequences of any kind at once: |
||
< |
<syntaxhighlight lang="racket"> |
||
#lang racket |
#lang racket |
||
Line 3,288: | Line 3,288: | ||
[i (in-naturals 1)]) ; 1, 2, ... infinitely |
[i (in-naturals 1)]) ; 1, 2, ... infinitely |
||
(printf "~s: ~s ~s ~s\n" i x y z)) |
(printf "~s: ~s ~s ~s\n" i x y z)) |
||
</syntaxhighlight> |
|||
</lang> |
|||
The loop stops as soon as the first sequence terminates -- in the above |
The loop stops as soon as the first sequence terminates -- in the above |
||
Line 3,302: | Line 3,302: | ||
=== Basic functionality === |
=== Basic functionality === |
||
<lang |
<syntaxhighlight lang="raku" line>for <a b c> Z <A B C> Z 1, 2, 3 -> ($x, $y, $z) { |
||
say $x, $y, $z; |
say $x, $y, $z; |
||
}</ |
}</syntaxhighlight> |
||
The <code>Z</code> operator stops emitting items as soon as the shortest input list is exhausted. However, short lists are easily extended by replicating all or part of the list, or by appending any kind of lazy list generator to supply default values as necessary. |
The <code>Z</code> operator stops emitting items as soon as the shortest input list is exhausted. However, short lists are easily extended by replicating all or part of the list, or by appending any kind of lazy list generator to supply default values as necessary. |
||
Line 3,314: | Line 3,314: | ||
Note that we can also factor out the concatenation by making the <tt>Z</tt> metaoperator apply the <tt>~</tt> concatenation operator across each triple: |
Note that we can also factor out the concatenation by making the <tt>Z</tt> metaoperator apply the <tt>~</tt> concatenation operator across each triple: |
||
<lang |
<syntaxhighlight lang="raku" line>.say for <a b c> Z~ <A B C> Z~ 1, 2, 3;</syntaxhighlight> |
||
We could also use the zip-to-string with the reduction metaoperator: |
We could also use the zip-to-string with the reduction metaoperator: |
||
<lang |
<syntaxhighlight lang="raku" line>.say for [Z~] <a b c>, <A B C>, (1,2,3);</syntaxhighlight> |
||
We could also write that out "long-hand": |
We could also write that out "long-hand": |
||
<lang |
<syntaxhighlight lang="raku" line>.say for zip :with(&infix:<~>), <a b c>, <A B C>, (1,2,3);</syntaxhighlight> |
||
returns the exact same result so if you aren't comfortable with the concise operators, you have a choice. |
returns the exact same result so if you aren't comfortable with the concise operators, you have a choice. |
||
Line 3,330: | Line 3,330: | ||
The common case of iterating over a list and a list of its indices can be done using the same method: |
The common case of iterating over a list and a list of its indices can be done using the same method: |
||
<lang |
<syntaxhighlight lang="raku" line>for ^Inf Z <a b c d> -> ($i, $letter) { ... }</syntaxhighlight> |
||
or by using the <code>.kv</code> (key and value) method on the list (and dropping the parentheses because the list returned by <code>.kv</code> is a flattened list): |
or by using the <code>.kv</code> (key and value) method on the list (and dropping the parentheses because the list returned by <code>.kv</code> is a flattened list): |
||
<lang |
<syntaxhighlight lang="raku" line>for <a b c d>.kv -> $i, $letter { ... }</syntaxhighlight> |
||
=== Iterate until all exhausted === |
=== Iterate until all exhausted === |
||
Line 3,340: | Line 3,340: | ||
If you have different sized lists that you want to pull a value from each per iteration, but want to continue until '''all''' of the lists are exhausted, we have <code>roundrobin</code>. |
If you have different sized lists that you want to pull a value from each per iteration, but want to continue until '''all''' of the lists are exhausted, we have <code>roundrobin</code>. |
||
<lang |
<syntaxhighlight lang="raku" line>.put for roundrobin <a b c>, 'A'..'G', ^5;</syntaxhighlight> |
||
{{out|yields}} |
{{out|yields}} |
||
<pre>a A 0 |
<pre>a A 0 |
||
Line 3,355: | Line 3,355: | ||
When a variable is used in a path notation, we put a colon in front of it. :counter |
When a variable is used in a path notation, we put a colon in front of it. :counter |
||
< |
<syntaxhighlight lang="red">>>blk: [["a" "b" "c"] ["A" "B" "C"] [1 2 3]] |
||
== [["a" "b" "c"] ["A" "B" "C"] [1 2 3]] |
== [["a" "b" "c"] ["A" "B" "C"] [1 2 3]] |
||
Line 3,361: | Line 3,361: | ||
a A 1 |
a A 1 |
||
b B 2 |
b B 2 |
||
c C 3</ |
c C 3</syntaxhighlight> |
||
=={{header|REXX}}== |
=={{header|REXX}}== |
||
Line 3,369: | Line 3,369: | ||
<br><br> |
<br><br> |
||
When ''all'' elements are blank, then it signifies the end of the arrays. |
When ''all'' elements are blank, then it signifies the end of the arrays. |
||
< |
<syntaxhighlight lang="rexx">/*REXX program shows how to simultaneously loop over multiple arrays.*/ |
||
x. = ' '; x.1 = "a"; x.2 = 'b'; x.3 = "c" |
x. = ' '; x.1 = "a"; x.2 = 'b'; x.3 = "c" |
||
y. = ' '; y.1 = "A"; y.2 = 'B'; y.3 = "C" |
y. = ' '; y.1 = "A"; y.2 = 'B'; y.3 = "C" |
||
Line 3,377: | Line 3,377: | ||
output = x.j || y.j || z.j |
output = x.j || y.j || z.j |
||
say output |
say output |
||
end /*j*/ /*stick a fork in it, we're done.*/</ |
end /*j*/ /*stick a fork in it, we're done.*/</syntaxhighlight> |
||
'''output''' |
'''output''' |
||
<pre> |
<pre> |
||
Line 3,388: | Line 3,388: | ||
In this example, two of the arrays are extended (past the 1<sup>st</sup> example). |
In this example, two of the arrays are extended (past the 1<sup>st</sup> example). |
||
<br>Also note that REXX doesn't require quotes around non-negative numbers (they're optional). |
<br>Also note that REXX doesn't require quotes around non-negative numbers (they're optional). |
||
< |
<syntaxhighlight lang="rexx">/*REXX program shows how to simultaneously loop over multiple arrays.*/ |
||
x.=' '; x.1="a"; x.2='b'; x.3="c"; x.4='d' |
x.=' '; x.1="a"; x.2='b'; x.3="c"; x.4='d' |
||
y.=' '; y.1="A"; y.2='B'; y.3="C"; |
y.=' '; y.1="A"; y.2='B'; y.3="C"; |
||
Line 3,396: | Line 3,396: | ||
output=x.j || y.j || z.j |
output=x.j || y.j || z.j |
||
say output |
say output |
||
end /*j*/ /*stick a fork in it, we're done.*/</ |
end /*j*/ /*stick a fork in it, we're done.*/</syntaxhighlight> |
||
'''output''' |
'''output''' |
||
<pre> |
<pre> |
||
Line 3,407: | Line 3,407: | ||
===dissimilar sized lists=== |
===dissimilar sized lists=== |
||
< |
<syntaxhighlight lang="rexx">/*REXX program shows how to simultaneously loop over multiple lists.*/ |
||
x = 'a b c d' |
x = 'a b c d' |
||
y = 'A B C' |
y = 'A B C' |
||
Line 3,414: | Line 3,414: | ||
output = word(x,j) || word(y,j) || word(z,j) |
output = word(x,j) || word(y,j) || word(z,j) |
||
say output |
say output |
||
end /*j*/ /*stick a fork in it, we're done.*/</ |
end /*j*/ /*stick a fork in it, we're done.*/</syntaxhighlight> |
||
'''output''' |
'''output''' |
||
<pre> |
<pre> |
||
Line 3,424: | Line 3,424: | ||
===idiomatic method for lists=== |
===idiomatic method for lists=== |
||
< |
<syntaxhighlight lang="rexx">/*REXX program shows how to simultaneously loop over multiple lists.*/ |
||
x = 'a b c d' |
x = 'a b c d' |
||
y = 'A B C' |
y = 'A B C' |
||
Line 3,430: | Line 3,430: | ||
do j=1 for max(words(x), words(y), words(z)) |
do j=1 for max(words(x), words(y), words(z)) |
||
say word(x,j) || word(y,j) || word(z,j) |
say word(x,j) || word(y,j) || word(z,j) |
||
end /*j*/ /*stick a fork in it, we're done.*/</ |
end /*j*/ /*stick a fork in it, we're done.*/</syntaxhighlight> |
||
'''output''' |
'''output''' |
||
<pre> |
<pre> |
||
Line 3,441: | Line 3,441: | ||
=={{header|Ring}}== |
=={{header|Ring}}== |
||
< |
<syntaxhighlight lang="ring"> |
||
array1 = ["a", "b", "c"] |
array1 = ["a", "b", "c"] |
||
array2 = ["A", "B", "C"] |
array2 = ["A", "B", "C"] |
||
Line 3,449: | Line 3,449: | ||
see array1[n] + array2[n] + array3[n] + nl |
see array1[n] + array2[n] + array3[n] + nl |
||
next |
next |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Ruby}}== |
=={{header|Ruby}}== |
||
< |
<syntaxhighlight lang="ruby">['a','b','c'].zip(['A','B','C'], [1,2,3]) {|i,j,k| puts "#{i}#{j}#{k}"}</syntaxhighlight> |
||
or |
or |
||
< |
<syntaxhighlight lang="ruby">['a','b','c'].zip(['A','B','C'], [1,2,3]) {|a| puts a.join}</syntaxhighlight> |
||
Both of these loops print <code>aA1</code>, <code>bB2</code>, <code>cC3</code>. |
Both of these loops print <code>aA1</code>, <code>bB2</code>, <code>cC3</code>. |
||
Line 3,461: | Line 3,461: | ||
If an argument array is longer, the excess elements are ignored. |
If an argument array is longer, the excess elements are ignored. |
||
If an argument array is shorter, the value <code>nil</code> is supplied. |
If an argument array is shorter, the value <code>nil</code> is supplied. |
||
< |
<syntaxhighlight lang="ruby">irb(main):001:0> ['a','b','c'].zip(['A','B'], [1,2,3,4]) {|a| puts a.join} |
||
aA1 |
aA1 |
||
bB2 |
bB2 |
||
Line 3,467: | Line 3,467: | ||
=> nil |
=> nil |
||
irb(main):002:0> ['a','b','c'].zip(['A','B'], [1,2,3,4]) |
irb(main):002:0> ['a','b','c'].zip(['A','B'], [1,2,3,4]) |
||
=> [["a", "A", 1], ["b", "B", 2], ["c", nil, 3]]</ |
=> [["a", "A", 1], ["b", "B", 2], ["c", nil, 3]]</syntaxhighlight> |
||
=={{header|Run BASIC}}== |
=={{header|Run BASIC}}== |
||
< |
<syntaxhighlight lang="runbasic">for i = 1 to 3 |
||
a$(i) = chr$(i+96) |
a$(i) = chr$(i+96) |
||
b$(i) = chr$(i+64) |
b$(i) = chr$(i+64) |
||
Line 3,478: | Line 3,478: | ||
for i = 1 to 3 |
for i = 1 to 3 |
||
print a$(i);b$(i);c(i) |
print a$(i);b$(i);c(i) |
||
next</ |
next</syntaxhighlight> |
||
=={{header|Rust}}== |
=={{header|Rust}}== |
||
< |
<syntaxhighlight lang="rust">fn main() { |
||
let a1 = ["a", "b", "c"]; |
let a1 = ["a", "b", "c"]; |
||
let a2 = ["A", "B", "C"]; |
let a2 = ["A", "B", "C"]; |
||
Line 3,490: | Line 3,490: | ||
println!("{}{}{}", x, y, z); |
println!("{}{}{}", x, y, z); |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>aA1 |
<pre>aA1 |
||
Line 3,497: | Line 3,497: | ||
=={{header|Salmon}}== |
=={{header|Salmon}}== |
||
< |
<syntaxhighlight lang="salmon">// First, we'll define a general-purpose zip() to zip |
||
any |
any |
||
// number of lists together. |
// number of lists together. |
||
Line 3,521: | Line 3,521: | ||
c := [1, 2, 3]; |
c := [1, 2, 3]; |
||
iterate (x; zip(a, b, c)) |
iterate (x; zip(a, b, c)) |
||
print(x[0], x[1], x[2], "\n");;</ |
print(x[0], x[1], x[2], "\n");;</syntaxhighlight> |
||
The preceding code will throw an exception if the lists aren't the same |
The preceding code will throw an exception if the lists aren't the same |
||
Line 3,529: | Line 3,529: | ||
some lists are shorter than the longest: |
some lists are shorter than the longest: |
||
< |
<syntaxhighlight lang="salmon">// First, we'll define a general-purpose zip() to zip |
||
any |
any |
||
// number of lists together. |
// number of lists together. |
||
Line 3,555: | Line 3,555: | ||
c := [1, 2, 3]; |
c := [1, 2, 3]; |
||
iterate (x; zip(a, b, c)) |
iterate (x; zip(a, b, c)) |
||
print(x[0], x[1], x[2], "\n");;</ |
print(x[0], x[1], x[2], "\n");;</syntaxhighlight> |
||
=={{header|Sather}}== |
=={{header|Sather}}== |
||
< |
<syntaxhighlight lang="sather">class MAIN is |
||
main is |
main is |
||
a :ARRAY{STR} := |"a", "b", "c"|; |
a :ARRAY{STR} := |"a", "b", "c"|; |
||
Line 3,568: | Line 3,568: | ||
end; |
end; |
||
end; |
end; |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Scala}}== |
=={{header|Scala}}== |
||
< |
<syntaxhighlight lang="scala"> |
||
("abc", "ABC", "123").zipped foreach { (x, y, z) => |
("abc", "ABC", "123").zipped foreach { (x, y, z) => |
||
println(x.toString + y + z) |
println(x.toString + y + z) |
||
} |
} |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Scheme}}== |
=={{header|Scheme}}== |
||
Line 3,585: | Line 3,585: | ||
into a new list. |
into a new list. |
||
< |
<syntaxhighlight lang="scheme"> |
||
(let ((a '("a" "b" "c")) |
(let ((a '("a" "b" "c")) |
||
(b '("A" "B" "C")) |
(b '("A" "B" "C")) |
||
Line 3,596: | Line 3,596: | ||
(newline)) |
(newline)) |
||
a b c)) |
a b c)) |
||
</syntaxhighlight> |
|||
</lang> |
|||
Scheme has a <code>vector</code> datatype with constant-time |
Scheme has a <code>vector</code> datatype with constant-time |
||
Line 3,603: | Line 3,603: | ||
and <code>vector-map</code>: |
and <code>vector-map</code>: |
||
< |
<syntaxhighlight lang="scheme"> |
||
(let ((a (vector "a" "b" "c")) |
(let ((a (vector "a" "b" "c")) |
||
(b (vector "A" "B" "C")) |
(b (vector "A" "B" "C")) |
||
Line 3,614: | Line 3,614: | ||
(newline)) |
(newline)) |
||
a b c)) |
a b c)) |
||
</syntaxhighlight> |
|||
</lang> |
|||
Note, the lists or vectors must all be of the same length. |
Note, the lists or vectors must all be of the same length. |
||
Line 3,620: | Line 3,620: | ||
=={{header|Sidef}}== |
=={{header|Sidef}}== |
||
The simplest way is by using the Array.zip{} method: |
The simplest way is by using the Array.zip{} method: |
||
< |
<syntaxhighlight lang="ruby">[%w(a b c),%w(A B C),%w(1 2 3)].zip { |i,j,k| |
||
say (i, j, k) |
say (i, j, k) |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 3,633: | Line 3,633: | ||
{{works with|GNU Smalltalk}} |
{{works with|GNU Smalltalk}} |
||
< |
<syntaxhighlight lang="smalltalk">|a b c| |
||
a := OrderedCollection new addAll: #('a' 'b' 'c'). |
a := OrderedCollection new addAll: #('a' 'b' 'c'). |
||
b := OrderedCollection new addAll: #('A' 'B' 'C'). |
b := OrderedCollection new addAll: #('A' 'B' 'C'). |
||
Line 3,642: | Line 3,642: | ||
(b at: i) display. |
(b at: i) display. |
||
(c at: i) displayNl. |
(c at: i) displayNl. |
||
].</ |
].</syntaxhighlight> |
||
If index ''i'' is out of bound, a runtime error is raised. |
If index ''i'' is out of bound, a runtime error is raised. |
||
Line 3,649: | Line 3,649: | ||
<br>Also, most Smalltalks (all?) can concatenate non-string args¹. |
<br>Also, most Smalltalks (all?) can concatenate non-string args¹. |
||
<br>At least in ST/X, the following works ¹: |
<br>At least in ST/X, the following works ¹: |
||
< |
<syntaxhighlight lang="smalltalk">|a b c| |
||
a := #('a' 'b' 'c'). |
a := #('a' 'b' 'c'). |
||
Line 3,656: | Line 3,656: | ||
1 to: (a size) do: [ :i | |
1 to: (a size) do: [ :i | |
||
((a at: i),(b at: i),(c at: i)) displayNl. |
((a at: i),(b at: i),(c at: i)) displayNl. |
||
].</ |
].</syntaxhighlight> |
||
Another alternative is to use a multi-collection enumerator, |
Another alternative is to use a multi-collection enumerator, |
||
which hides the element access (transparent to how elements are stored inside the collection): |
which hides the element access (transparent to how elements are stored inside the collection): |
||
< |
<syntaxhighlight lang="smalltalk">|a b c| |
||
a := #('a' 'b' 'c'). |
a := #('a' 'b' 'c'). |
||
Line 3,667: | Line 3,667: | ||
a with:b with:c do:[:ai :bi :ci | |
a with:b with:c do:[:ai :bi :ci | |
||
(ai,bi,ci) displayNl. |
(ai,bi,ci) displayNl. |
||
].</ |
].</syntaxhighlight> |
||
1) concatenation of integer objects as shown above may require a change in the <tt>,</tt> (comma) implementation, to send "asString" to the argument. |
1) concatenation of integer objects as shown above may require a change in the <tt>,</tt> (comma) implementation, to send "asString" to the argument. |
||
Line 3,674: | Line 3,674: | ||
The below code will combine arbitrarily many lists of strings |
The below code will combine arbitrarily many lists of strings |
||
into a single list with length equal to that of the shortest list. |
into a single list with length equal to that of the shortest list. |
||
<syntaxhighlight lang="standard ml"> |
|||
<lang Standard ML> |
|||
(* |
(* |
||
* val combine_lists : string list list -> string list |
* val combine_lists : string list list -> string list |
||
Line 3,684: | Line 3,684: | ||
(* ["a1Ax","b2By","c3Cz"] *) |
(* ["a1Ax","b2By","c3Cz"] *) |
||
combine_lists[["a","b","c"],["1","2","3"],["A","B","C"],["x","y","z"]]; |
combine_lists[["a","b","c"],["1","2","3"],["A","B","C"],["x","y","z"]]; |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Stata}}== |
=={{header|Stata}}== |
||
Use an index variable. |
Use an index variable. |
||
< |
<syntaxhighlight lang="stata">local u a b c |
||
local v A B C |
local v A B C |
||
matrix w=1,2,3 |
matrix w=1,2,3 |
||
forv i=1/3 { |
forv i=1/3 { |
||
di "`: word `i' of `u''`: word `i' of `v''`=el("w",1,`i')'" |
di "`: word `i' of `u''`: word `i' of `v''`=el("w",1,`i')'" |
||
}</ |
}</syntaxhighlight> |
||
=== Mata === |
=== Mata === |
||
< |
<syntaxhighlight lang="stata">mata |
||
u="a","b","c" |
u="a","b","c" |
||
v="A","B","C" |
v="A","B","C" |
||
Line 3,706: | Line 3,706: | ||
printf("%s%s%f\n",u[i],v[i],w[i]) |
printf("%s%s%f\n",u[i],v[i],w[i]) |
||
} |
} |
||
end</ |
end</syntaxhighlight> |
||
=={{header|SuperCollider}}== |
=={{header|SuperCollider}}== |
||
Using three variables and indexing (SuperCollider posts the last statement in the REPL) |
Using three variables and indexing (SuperCollider posts the last statement in the REPL) |
||
<syntaxhighlight lang="supercollider"> |
|||
<lang SuperCollider> |
|||
#x, y, z = [["a", "b", "c"], ["A", "B", "C"], ["1", "2", "3"]]; |
#x, y, z = [["a", "b", "c"], ["A", "B", "C"], ["1", "2", "3"]]; |
||
3.collect { |i| x[i] ++ y[i] ++ z[i] } |
3.collect { |i| x[i] ++ y[i] ++ z[i] } |
||
</syntaxhighlight> |
|||
</lang> |
|||
A more idiomatic way of writing it, independent of the number of dimensions: |
A more idiomatic way of writing it, independent of the number of dimensions: |
||
<syntaxhighlight lang="supercollider"> |
|||
<lang SuperCollider> |
|||
[["a", "b", "c"], ["A", "B", "C"], ["1", "2", "3"]].flop.collect { |x| x.join } |
[["a", "b", "c"], ["A", "B", "C"], ["1", "2", "3"]].flop.collect { |x| x.join } |
||
</syntaxhighlight> |
|||
</lang> |
|||
Or simpler: |
Or simpler: |
||
<syntaxhighlight lang="supercollider"> |
|||
<lang SuperCollider> |
|||
[["a", "b", "c"], ["A", "B", "C"], ["1", "2", "3"]].flop.collect(_.join) |
[["a", "b", "c"], ["A", "B", "C"], ["1", "2", "3"]].flop.collect(_.join) |
||
</syntaxhighlight> |
|||
</lang> |
|||
Same with lamination (a concept from APL/[http://rosettacode.org/wiki/Category:J#The_J_language J]): |
Same with lamination (a concept from APL/[http://rosettacode.org/wiki/Category:J#The_J_language J]): |
||
<syntaxhighlight lang="supercollider"> |
|||
<lang SuperCollider> |
|||
["a", "b", "c"] +++ ["A", "B", "C"] +++ ["1", "2", "3"] |
["a", "b", "c"] +++ ["A", "B", "C"] +++ ["1", "2", "3"] |
||
</syntaxhighlight> |
|||
</lang> |
|||
Independent of dimensions: |
Independent of dimensions: |
||
<syntaxhighlight lang="supercollider"> |
|||
<lang SuperCollider> |
|||
[["a", "b", "c"], ["A", "B", "C"], ["1", "2", "3"]].reduce('+++') |
[["a", "b", "c"], ["A", "B", "C"], ["1", "2", "3"]].reduce('+++') |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Swift}}== |
=={{header|Swift}}== |
||
< |
<syntaxhighlight lang="swift">let a1 = ["a", "b", "c"] |
||
let a2 = ["A", "B", "C"] |
let a2 = ["A", "B", "C"] |
||
let a3 = [1, 2, 3] |
let a3 = [1, 2, 3] |
||
Line 3,744: | Line 3,744: | ||
for i in 0 ..< a1.count { |
for i in 0 ..< a1.count { |
||
println("\(a1[i])\(a2[i])\(a3[i])") |
println("\(a1[i])\(a2[i])\(a3[i])") |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>aA1 |
<pre>aA1 |
||
Line 3,752: | Line 3,752: | ||
=={{header|Tailspin}}== |
=={{header|Tailspin}}== |
||
Simplest iteration with an ordinary "loop" that will error on uneven sizes |
Simplest iteration with an ordinary "loop" that will error on uneven sizes |
||
< |
<syntaxhighlight lang="tailspin"> |
||
def x: ['a', 'b', 'c']; |
def x: ['a', 'b', 'c']; |
||
def y: ['A', 'B', 'C']; |
def y: ['A', 'B', 'C']; |
||
Line 3,759: | Line 3,759: | ||
1..$x::length -> '$x($);$y($);$z($); |
1..$x::length -> '$x($);$y($);$z($); |
||
' -> !OUT::write |
' -> !OUT::write |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 3,767: | Line 3,767: | ||
</pre> |
</pre> |
||
A simple transpose method that gives the same output and also errors on uneven sizes |
A simple transpose method that gives the same output and also errors on uneven sizes |
||
< |
<syntaxhighlight lang="tailspin"> |
||
templates transpose |
templates transpose |
||
def a: $; |
def a: $; |
||
Line 3,776: | Line 3,776: | ||
[$x, $y, $z] -> transpose... -> '$...; |
[$x, $y, $z] -> transpose... -> '$...; |
||
' -> !OUT::write |
' -> !OUT::write |
||
</syntaxhighlight> |
|||
</lang> |
|||
A more complex transpose that uses "foreach" more in line with the task proposal and handles uneven arrays |
A more complex transpose that uses "foreach" more in line with the task proposal and handles uneven arrays |
||
< |
<syntaxhighlight lang="tailspin"> |
||
def u: ['a', 'b']; |
def u: ['a', 'b']; |
||
def v: ['A', 'B', 'C']; |
def v: ['A', 'B', 'C']; |
||
Line 3,799: | Line 3,799: | ||
[$u,$v,$w] -> transpose2... -> '$...; |
[$u,$v,$w] -> transpose2... -> '$...; |
||
' -> !OUT::write |
' -> !OUT::write |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 3,812: | Line 3,812: | ||
=={{header|Tcl}}== |
=={{header|Tcl}}== |
||
< |
<syntaxhighlight lang="tcl">set list1 {a b c} |
||
set list2 {A B C} |
set list2 {A B C} |
||
set list3 {1 2 3} |
set list3 {1 2 3} |
||
foreach i $list1 j $list2 k $list3 { |
foreach i $list1 j $list2 k $list3 { |
||
puts "$i$j$k" |
puts "$i$j$k" |
||
}</ |
}</syntaxhighlight> |
||
If lists are different lengths, the manual |
If lists are different lengths, the manual |
||
[http://www.tcl.tk/man/tcl8.5/TclCmd/foreach.htm] says: |
[http://www.tcl.tk/man/tcl8.5/TclCmd/foreach.htm] says: |
||
Line 3,828: | Line 3,828: | ||
=={{header|TorqueScript}}== |
=={{header|TorqueScript}}== |
||
<syntaxhighlight lang="torquescript"> |
|||
<lang Torquescript> |
|||
$var[0] = "a b c" |
$var[0] = "a b c" |
||
$var[1] = "A B C"; |
$var[1] = "A B C"; |
||
Line 3,835: | Line 3,835: | ||
for(%i=0;%i<3;%i++) |
for(%i=0;%i<3;%i++) |
||
echo(getWord($var[0],%i) @ getWord($var[1],%i) @ getWord($var[2],%i)); |
echo(getWord($var[0],%i) @ getWord($var[1],%i) @ getWord($var[2],%i)); |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|TUSCRIPT}}== |
=={{header|TUSCRIPT}}== |
||
< |
<syntaxhighlight lang="tuscript"> |
||
$$ MODE TUSCRIPT |
$$ MODE TUSCRIPT |
||
arr1="a'b'c" |
arr1="a'b'c" |
||
Line 3,846: | Line 3,846: | ||
PRINT a,b,c |
PRINT a,b,c |
||
ENDLOOP |
ENDLOOP |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 3,858: | Line 3,858: | ||
===Pattern language=== |
===Pattern language=== |
||
< |
<syntaxhighlight lang="bash">$ txr -c '@(bind a ("a" "b" "c")) |
||
@(bind b ("A" "B" "C")) |
@(bind b ("A" "B" "C")) |
||
@(bind c ("1" "2" "3")) |
@(bind c ("1" "2" "3")) |
||
Line 3,868: | Line 3,868: | ||
aA1 |
aA1 |
||
bB2 |
bB2 |
||
cC3</ |
cC3</syntaxhighlight> |
||
===TXR Lisp, using <code>mapcar</code>=== |
===TXR Lisp, using <code>mapcar</code>=== |
||
Line 3,876: | Line 3,876: | ||
finally printed in one go. |
finally printed in one go. |
||
< |
<syntaxhighlight lang="bash">$ txr -e '(pprint (mappend (op list) "abc" "ABC" "123" |
||
(repeat "\n")))' |
(repeat "\n")))' |
||
aA1 |
aA1 |
||
bB2 |
bB2 |
||
cC3</ |
cC3</syntaxhighlight> |
||
===TXR Lisp, using <code>each</code>=== |
===TXR Lisp, using <code>each</code>=== |
||
< |
<syntaxhighlight lang="bash">$ txr -e '(each ((x "abc") (y "ABC") (z "123")) |
||
(put-line `@x@y@z`))' |
(put-line `@x@y@z`))' |
||
aA1 |
aA1 |
||
bB2 |
bB2 |
||
cC3</ |
cC3</syntaxhighlight> |
||
===Translation of Scheme=== |
===Translation of Scheme=== |
||
Line 3,894: | Line 3,894: | ||
{{trans|Scheme}} |
{{trans|Scheme}} |
||
< |
<syntaxhighlight lang="txrlisp">;; Scheme's vector-for-each: a one-liner in TXR |
||
;; that happily works over strings and lists. |
;; that happily works over strings and lists. |
||
;; We don't need "srfi-43". |
;; We don't need "srfi-43". |
||
Line 3,915: | Line 3,915: | ||
(display i3) |
(display i3) |
||
(newline)) |
(newline)) |
||
a b c))</ |
a b c))</syntaxhighlight> |
||
===Translation of Logo=== |
===Translation of Logo=== |
||
Line 3,921: | Line 3,921: | ||
{{trans|Logo}} |
{{trans|Logo}} |
||
< |
<syntaxhighlight lang="txrlisp">(macro-time |
||
(defun question-var-to-meta-num (var) |
(defun question-var-to-meta-num (var) |
||
^(sys:var ,(int-str (cdr (symbol-name var)))))) |
^(sys:var ,(int-str (cdr (symbol-name var)))))) |
||
Line 3,936: | Line 3,936: | ||
(defun show (x) (pprinl x)) |
(defun show (x) (pprinl x)) |
||
(show (map [(word ?1 ?2 ?3)] [a b c] [A B C] [1 2 3]))</ |
(show (map [(word ?1 ?2 ?3)] [a b c] [A B C] [1 2 3]))</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 3,942: | Line 3,942: | ||
== {{header|TypeScript}} == |
== {{header|TypeScript}} == |
||
< |
<syntaxhighlight lang="javascript">// Loop over multiple arrays simultaneously |
||
var arr1: string[] = ['a', 'b', 'c']; |
var arr1: string[] = ['a', 'b', 'c']; |
||
var arr2: string[] = ['A', 'B', 'C']; |
var arr2: string[] = ['A', 'B', 'C']; |
||
Line 3,948: | Line 3,948: | ||
for (var i = 0; i <= 2; i++) |
for (var i = 0; i <= 2; i++) |
||
console.log(`${arr1[i]}${arr2[i]}${arr3[i]}`); |
console.log(`${arr1[i]}${arr2[i]}${arr3[i]}`); |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 3,966: | Line 3,966: | ||
{{works with|Bourne Shell}} |
{{works with|Bourne Shell}} |
||
< |
<syntaxhighlight lang="bash">a=a:b:c |
||
b=A:B:C |
b=A:B:C |
||
c=1:2:3 |
c=1:2:3 |
||
Line 3,981: | Line 3,981: | ||
i=`expr $i + 1` |
i=`expr $i + 1` |
||
done |
done |
||
IFS=$oldifs</ |
IFS=$oldifs</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 4,003: | Line 4,003: | ||
{{works with|Bourne Shell}} |
{{works with|Bourne Shell}} |
||
< |
<syntaxhighlight lang="bash">A='a1 a2 a3' |
||
B='b1 b2 b3' |
B='b1 b2 b3' |
||
Line 4,011: | Line 4,011: | ||
printf "$a $1\n" |
printf "$a $1\n" |
||
shift |
shift |
||
done</ |
done</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 4,027: | Line 4,027: | ||
{{works with|ksh93}} |
{{works with|ksh93}} |
||
< |
<syntaxhighlight lang="bash">a=(a b c) |
||
b=(A B C) |
b=(A B C) |
||
c=(1 2 3) |
c=(1 2 3) |
||
for ((i = 0; i < ${#a[@]}; i++)); do |
for ((i = 0; i < ${#a[@]}; i++)); do |
||
echo "${a[$i]}${b[$i]}${c[$i]}" |
echo "${a[$i]}${b[$i]}${c[$i]}" |
||
done</ |
done</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 4,044: | Line 4,044: | ||
{{works with|pdksh}} |
{{works with|pdksh}} |
||
< |
<syntaxhighlight lang="bash">set -A a a b c |
||
set -A b A B C |
set -A b A B C |
||
set -A c 1 2 3 |
set -A c 1 2 3 |
||
Line 4,051: | Line 4,051: | ||
echo "${a[$i]}${b[$i]}${c[$i]}" |
echo "${a[$i]}${b[$i]}${c[$i]}" |
||
((i++)) |
((i++)) |
||
done</ |
done</syntaxhighlight> |
||
{{works with|zsh}} |
{{works with|zsh}} |
||
< |
<syntaxhighlight lang="bash">a=(a b c) |
||
b=(A B C) |
b=(A B C) |
||
c=(1 2 3) |
c=(1 2 3) |
||
for ((i = 1; i <= $#a; i++)); do |
for ((i = 1; i <= $#a; i++)); do |
||
echo "$a[$i]$b[$i]$c[$i]" |
echo "$a[$i]$b[$i]$c[$i]" |
||
done</ |
done</syntaxhighlight> |
||
==={{header|C Shell}}=== |
==={{header|C Shell}}=== |
||
Line 4,067: | Line 4,067: | ||
shell to exit with an error like ''b: Subscript out of range.'' |
shell to exit with an error like ''b: Subscript out of range.'' |
||
< |
<syntaxhighlight lang="csh">set a=(a b c) |
||
set b=(A B C) |
set b=(A B C) |
||
set c=(1 2 3) |
set c=(1 2 3) |
||
Line 4,074: | Line 4,074: | ||
echo "$a[$i]$b[$i]$c[$i]" |
echo "$a[$i]$b[$i]$c[$i]" |
||
@ i += 1 |
@ i += 1 |
||
end</ |
end</syntaxhighlight> |
||
=={{header|Ursa}}== |
=={{header|Ursa}}== |
||
Looping over multiple arrays in an interactive session: |
Looping over multiple arrays in an interactive session: |
||
< |
<syntaxhighlight lang="ursa">> decl string<> a b c |
||
> append (split "abc" "") a |
> append (split "abc" "") a |
||
> append (split "ABC" "") b |
> append (split "ABC" "") b |
||
Line 4,088: | Line 4,088: | ||
bB2 |
bB2 |
||
cC3 |
cC3 |
||
> _</ |
> _</syntaxhighlight> |
||
If either of the arrays are smaller than (size a), then an indexerror is thrown. This could be caught with a <code>try...catch</code> block. |
If either of the arrays are smaller than (size a), then an indexerror is thrown. This could be caught with a <code>try...catch</code> block. |
||
Line 4,094: | Line 4,094: | ||
Compute the transpose of the list formed of the three lists. |
Compute the transpose of the list formed of the three lists. |
||
If they're of unequal lengths, an exception occurs. |
If they're of unequal lengths, an exception occurs. |
||
< |
<syntaxhighlight lang="ursala">#show+ |
||
main = ~&K7 <'abc','ABC','123'></ |
main = ~&K7 <'abc','ABC','123'></syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 4,105: | Line 4,105: | ||
=={{header|Vala}}== |
=={{header|Vala}}== |
||
< |
<syntaxhighlight lang="vala">const char a1[] = {'a','b','c'}; |
||
const char a2[] = {'A','B','C'}; |
const char a2[] = {'A','B','C'}; |
||
const int a3[] = {1, 2, 3}; |
const int a3[] = {1, 2, 3}; |
||
Line 4,112: | Line 4,112: | ||
for (int i = 0; i < 3; i++) |
for (int i = 0; i < 3; i++) |
||
stdout.printf("%c%c%i\n", a1[i], a2[i], a3[i]); |
stdout.printf("%c%c%i\n", a1[i], a2[i], a3[i]); |
||
}</ |
}</syntaxhighlight> |
||
=={{header|VBA}}== |
=={{header|VBA}}== |
||
{{works with|VBA|VBA Excel 2013}} |
{{works with|VBA|VBA Excel 2013}} |
||
< |
<syntaxhighlight lang="vb">' Loop over multiple arrays simultaneously - VBA - 08/02/2021 |
||
Sub Main() |
Sub Main() |
||
Line 4,126: | Line 4,126: | ||
Next |
Next |
||
Debug.Print Mid(buf,3) |
Debug.Print Mid(buf,3) |
||
End Sub </ |
End Sub </syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 4,135: | Line 4,135: | ||
=={{header|VBScript}}== |
=={{header|VBScript}}== |
||
< |
<syntaxhighlight lang="vb">' Loop over multiple arrays simultaneously - VBScript - 08/02/2021 |
||
a = Array("a","b","c") |
a = Array("a","b","c") |
||
Line 4,143: | Line 4,143: | ||
buf = buf & vbCrLf & a(i) & b(i) & c(i) |
buf = buf & vbCrLf & a(i) & b(i) & c(i) |
||
Next |
Next |
||
WScript.Echo Mid(buf,3) </ |
WScript.Echo Mid(buf,3) </syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 4,154: | Line 4,154: | ||
=={{header|Visual Basic .NET}}== |
=={{header|Visual Basic .NET}}== |
||
Two implementations: one determines the shortest of the arrays and uses a simple For loop with element accesses to each array separately; one uses Enumerable.Zip (which can only zip two sequences at once) twice to create 3-tuples. Enumerable.Zip stops when either source runs out of elements, so the behavior of the two implementations is identical for arrays of different lengths. |
Two implementations: one determines the shortest of the arrays and uses a simple For loop with element accesses to each array separately; one uses Enumerable.Zip (which can only zip two sequences at once) twice to create 3-tuples. Enumerable.Zip stops when either source runs out of elements, so the behavior of the two implementations is identical for arrays of different lengths. |
||
< |
<syntaxhighlight lang="vbnet"> |
||
Module Program |
Module Program |
||
Sub Main() |
Sub Main() |
||
Line 4,172: | Line 4,172: | ||
Next |
Next |
||
End Sub |
End Sub |
||
End Module</ |
End Module</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 4,184: | Line 4,184: | ||
=={{header|Visual FoxPro}}== |
=={{header|Visual FoxPro}}== |
||
< |
<syntaxhighlight lang="vfp"> |
||
LOCAL i As Integer, n As Integer, c As String |
LOCAL i As Integer, n As Integer, c As String |
||
LOCAL ARRAY a1[3], a2[3], a3[4], a[3] |
LOCAL ARRAY a1[3], a2[3], a3[4], a[3] |
||
Line 4,221: | Line 4,221: | ||
? "Solution using a cursor" |
? "Solution using a cursor" |
||
LIST OFF FIELDS c4 |
LIST OFF FIELDS c4 |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 4,239: | Line 4,239: | ||
=={{header|Vlang}}== |
=={{header|Vlang}}== |
||
< |
<syntaxhighlight lang="vlang">fn main() { |
||
arrays := [['a','b','c'],['A','B','C'],['1','2','3']] |
arrays := [['a','b','c'],['A','B','C'],['1','2','3']] |
||
for i in 0..arrays[0].len { |
for i in 0..arrays[0].len { |
||
println('${arrays[0][i]}${arrays[1][i]}${arrays[2][i]}') |
println('${arrays[0][i]}${arrays[1][i]}${arrays[2][i]}') |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
=={{header|Wart}}== |
=={{header|Wart}}== |
||
< |
<syntaxhighlight lang="wart">each (x X n) (zip '(a b c) '(A B C) '(1 2 3)) |
||
prn x X n</ |
prn x X n</syntaxhighlight> |
||
=={{header|Wren}}== |
=={{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. |
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="ecmascript">var a1 = ["a", "b", "c"] |
||
var a2 = ["A", "B", "C"] |
var a2 = ["A", "B", "C"] |
||
var a3 = [1, 2, 3] |
var a3 = [1, 2, 3] |
||
for (i in a3) System.print("%(a1[i-1])%(a2[i-1])%(i)")</ |
for (i in a3) System.print("%(a1[i-1])%(a2[i-1])%(i)")</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 4,267: | Line 4,267: | ||
{{works with|nasm}} |
{{works with|nasm}} |
||
{{works with|windows}} |
{{works with|windows}} |
||
< |
<syntaxhighlight lang="asm"> |
||
extern _printf |
extern _printf |
||
Line 4,319: | Line 4,319: | ||
xor eax, eax |
xor eax, eax |
||
ret |
ret |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|XBasic}}== |
=={{header|XBasic}}== |
||
{{works with|Windows XBasic}} |
{{works with|Windows XBasic}} |
||
< |
<syntaxhighlight lang="xbasic">' Loop over multiple arrays simultaneously |
||
PROGRAM "loopoverarrays" |
PROGRAM "loopoverarrays" |
||
Line 4,338: | Line 4,338: | ||
END FUNCTION |
END FUNCTION |
||
END PROGRAM |
END PROGRAM |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|XPL0}}== |
=={{header|XPL0}}== |
||
< |
<syntaxhighlight lang="xpl0">string 0; \use zero terminated strings |
||
include c:\cxpl\codes; \intrinsic 'code' declarations |
include c:\cxpl\codes; \intrinsic 'code' declarations |
||
char A1, A2; |
char A1, A2; |
||
Line 4,354: | Line 4,354: | ||
CrLf(0); |
CrLf(0); |
||
]; |
]; |
||
]</ |
]</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 4,364: | Line 4,364: | ||
=={{header|Z80 Assembly}}== |
=={{header|Z80 Assembly}}== |
||
< |
<syntaxhighlight lang="z80">org &1000 |
||
Line 4,391: | Line 4,391: | ||
db "ABC" |
db "ABC" |
||
array3: |
array3: |
||
db "123"</ |
db "123"</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 4,406: | Line 4,406: | ||
=={{header|zkl}}== |
=={{header|zkl}}== |
||
< |
<syntaxhighlight lang="zkl">foreach a,b,c in (["a".."c"].zip(T("A","B","C"),[1..])){ println(a,b,c) }</syntaxhighlight> |
||
or |
or |
||
< |
<syntaxhighlight lang="zkl">Utils.zipWith(False,fcn{vm.arglist.concat().println()}, |
||
["a".."c"],T("A","B","C"),[1..])</ |
["a".."c"],T("A","B","C"),[1..])</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 4,420: | Line 4,420: | ||
=={{header|Zig}}== |
=={{header|Zig}}== |
||
< |
<syntaxhighlight lang="zig">const std = @import("std"); |
||
const a1: []const u8 = &[_]u8{ 'a', 'b', 'c' }; |
const a1: []const u8 = &[_]u8{ 'a', 'b', 'c' }; |
||
Line 4,429: | Line 4,429: | ||
for (a1) |_, i| |
for (a1) |_, i| |
||
try std.io.getStdOut().writer().print("{c} {c} {d}\n", .{ a1[i], a2[i], a3[i] }); |
try std.io.getStdOut().writer().print("{c} {c} {d}\n", .{ a1[i], a2[i], a3[i] }); |
||
}</ |
}</syntaxhighlight> |
||
=={{header|ZX Spectrum Basic}}== |
=={{header|ZX Spectrum Basic}}== |
||
< |
<syntaxhighlight lang="zxbasic">10 LET sza = 3: REM size of a |
||
20 LET szb = 3: REM size of b |
20 LET szb = 3: REM size of b |
||
30 LET szc = 3: REM size of c |
30 LET szc = 3: REM size of c |
||
Line 4,453: | Line 4,453: | ||
170 PRINT "and C$ runs down the right." |
170 PRINT "and C$ runs down the right." |
||
180 STOP |
180 STOP |
||
200 DATA "a","b","c","A","B","C","1","2","3"</ |
200 DATA "a","b","c","A","B","C","1","2","3"</syntaxhighlight> |
||
Simplification |
Simplification |
||
< |
<syntaxhighlight lang="zxbasic">10 READ size: DIM a$(size): DIM b$(size): DIM c$(size) |
||
20 FOR i=1 TO size |
20 FOR i=1 TO size |
||
30 READ a$(i),b$(i),c$(i) |
30 READ a$(i),b$(i),c$(i) |
||
40 PRINT a$(i);b$(i);c$(i) |
40 PRINT a$(i);b$(i);c$(i) |
||
50 NEXT i |
50 NEXT i |
||
60 DATA 3,"a","A","1","b","B","2","c","C","3"</ |
60 DATA 3,"a","A","1","b","B","2","c","C","3"</syntaxhighlight> |