Loop over multiple arrays simultaneously

From Rosetta Code
Jump to: navigation, search
Task
Loop over multiple arrays simultaneously
You are encouraged to solve this task according to the task description, using any language you may know.
Loop over multiple arrays (or lists or tuples or whatever they're called in your language) and print the ith element of each. Use your language's "for each" loop if it has one, otherwise iterate through the collection in order with some other loop.

For this example, loop over the arrays (a,b,c), (A,B,C) and (1,2,3) to produce the output

aA1
bB2
cC3

If possible, also describe what happens when the arrays are of different lengths.

Contents

[edit] ACL2

(defun print-lists (xs ys zs)
(if (or (endp xs) (endp ys) (endp zs))
nil
(progn$ (cw (first xs))
(cw "~x0~x1~%"
(first ys)
(first zs))
(print-lists (rest xs)
(rest ys)
(rest zs)))))
 
(print-lists '("a" "b" "c") '(A B C) '(1 2 3))

[edit] Ada

with Ada.Text_IO;  use Ada.Text_IO;
 
procedure Array_Loop_Test is
type Array_Index is range 1..3;
A1 : array (Array_Index) of Character := "abc";
A2 : array (Array_Index) of Character := "ABC";
A3 : array (Array_Index) of Integer  := (1, 2, 3);
begin
for Index in Array_Index'Range loop
Put_Line (A1 (Index) & A2 (Index) & Integer'Image (A3 (Index))(2));
end loop;
end Array_Loop_Test;

[edit] ALGOL 68

Works with: ALGOL 68 version Revision 1 - no extensions to language used
Works with: ALGOL 68G version Any - tested with release 1.18.0-9h.tiny
[]UNION(CHAR,INT) x=("a","b","c"), y=("A","B","C"), z=(1,2,3);
FOR i TO UPB x DO
printf(($ggd$, x[i], y[i], z[i], $l$))
OD

Output:

aA1
bB2
cC3

[edit] AWK

BEGIN {
split("a,b,c", a, ",");
split("A,B,C", b, ",");
split("1,2,3", c, ",");
 
for(i = 1; i <= length(a); i++) {
print a[i] b[i] c[i];
}
}

[edit] AutoHotkey

[edit] Pseudo-arrays

StringSplit creates a pseudo-array

List1 = a,b,c
List2 = A,B,C
List3 = 1,2,3
MsgBox, % LoopMultiArrays()
 
List1 = a,b,c,d,e
List2 = A,B,C,D
List3 = 1,2,3
MsgBox, % LoopMultiArrays()
 
 
;---------------------------------------------------------------------------
LoopMultiArrays() { ; print the ith element of each
;---------------------------------------------------------------------------
local Result
StringSplit, List1_, List1, `,
StringSplit, List2_, List2, `,
StringSplit, List3_, List3, `,
Loop, % List1_0
Result .= List1_%A_Index% List2_%A_Index% List3_%A_Index% "`n"
Return, Result
}

An array that is too short on creation will return empty strings when trying to retrieve further elements. The 2nd Message box shows:

aA1
bB2
cC3
dD
e

[edit] Real arrays

Works with: AutoHotkey_L

In AutoHotkey_L, we can use true arrays (Objects) and the For loop.

List1 := ["a", "b", "c"]
List2 := ["A", "B", "C"]
List3 := [ 1 , 2 , 3 ]
MsgBox, % LoopMultiArrays()
 
List1 := ["a", "b", "c", "d", "e"]
List2 := ["A", "B", "C", "D"]
List3 := [1,2,3]
MsgBox, % LoopMultiArrays()
 
 
LoopMultiArrays() {
local Result
For key, value in List1
Result .= value . List2[key] . List3[key] "`n"
Return, Result
}
The output from this script is identical to the first one.

[edit] Babel

There are two ways to do this in Babel. First, you could transpose the lists:

main: { (('a' 'b' 'c')('A' 'B' 'C')('1' '2' '3')) simul_array }
 
simul_array!:
{ trans
{ { << } each "\n" << } each }

The 'trans' operator substitutes nil in the portions of each transposed column wherever a row list was shorter than the longest row list. The '<<' operator prints nothing if the top-of-stack is nil.

A more literal solution to the problem as presented would be to iterate across each list using a user-defined cdrall operator:

main: { (('a' 'b' 'c')('A' 'B' 'C')('1' '2' '3')) simul_array }
 
simul_array!:
{{ dup
{ car << } each
cdrall }
{ allnil? not }
while }
 
cdrall!: { { { cdr } each -1 take } nest }
 
-- only returns true if all elements of a list are nil
allnil?!:
{ 1 <->
{ car nil?
{ zap 0 last }
{ nil }
if} each }

This solution is formally identical to the first and will handle lists of varying lengths by printing inserting nil and printing nothing for the tail ends of the short lists.

[edit] BBC BASIC

      DIM array1$(2), array2$(2), array3%(2)
array1$() = "a", "b", "c"
array2$() = "A", "B", "C"
array3%() = 1, 2, 3
 
FOR index% = 0 TO 2
PRINT array1$(index%) ; array2$(index%) ; array3%(index%)
NEXT

[edit] C

Given several arrays, especially if they are heterogeneous, the most ordinary way to loop over all of them is to simply use an index variable. Determining when to stop is generally done in some application-specific way.

#include <stdio.h>
 
char a1[] = {'a','b','c'};
char a2[] = {'A','B','C'};
int a3[] = {1,2,3};
 
int main(void) {
for (int i = 0; i < 3; i++) {
printf("%c%c%i\n", a1[i], a2[i], a3[i]);
}
}

(Note: Some compilers may require a flag to accept this modern C code, such as gcc -std=c99.)

On the other hand, it is possible to write a more generic higher-order iteration scheme, as demonstrated in the following example. Here, a type for arrays with runtime-specified lengths and polymorphic printing is defined, and the iteration continues up to the length of the shortest array.

#include <stdio.h>
 
typedef struct closure {
void (*f)( void *elem, void *data);
void *data;
} *Closure;
 
typedef struct listSpec{
void (*print)(const void *);
void *ary;
int count;
size_t elem_size;
} *ListSpec;
 
#define LIST_SPEC( pf,typ,aa) {pf, aa, (sizeof(aa)/sizeof(typ)), sizeof(typ) }
 
 
/* calls closure's function f for each element in list */
void DoForEach( Closure c, ListSpec aspec )
{
int i;
void *val_ptr;
for (i=0, val_ptr=aspec->ary; i< aspec->count; i++) {
(*c->f)(val_ptr, c->data);
val_ptr = ((char *)val_ptr) + aspec->elem_size;
}
}
 
/* Used to find the minimum array length of list of lists */
void FindMin( ListSpec *paspec, int *minCount )
{
ListSpec aspec = *paspec;
if (*minCount>aspec->count) *minCount = aspec->count;
}
 
/* prints an element of a list using the list's print function */
void PrintElement( ListSpec *paspec, int *indx )
{
ListSpec aspec = *paspec;
(*aspec->print)( ((char*)aspec->ary) + (*indx)*aspec->elem_size);
}
 
 
/* Loop Over multiple lists (a list of lists)*/
void LoopMultiple( ListSpec arrays)
{
int indx;
int minCount = 100000;
struct closure c1 = { &FindMin, &minCount };
struct closure xclsr = { &PrintElement, &indx };
DoForEach( &c1, arrays);
printf("min count = %d\n", minCount);
 
for (indx=0; indx<minCount; indx++) {
DoForEach(&xclsr, arrays );
printf("\n");
}
}
 
/* Defining our Lists */
 
void PrintInt(const int *ival)
{ printf("%3d,", *ival);
}
int ary1[] = { 6,5,4,9,8,7 };
struct listSpec lspec1 = LIST_SPEC( &PrintInt, int, ary1 );
 
void PrintShort(const short *ival)
{ printf("%3d,", *ival);
}
short ary2[] = { 3, 66, 20, 15, 7, 22, 10 };
struct listSpec lspec2 = LIST_SPEC( &PrintShort , short, ary2 );
 
void PrintStrg(const char *const *strg )
{ printf(" %s", *strg);
}
const char *ary3[] = {"Hello", "all", "you","good", "folks","out", "there" };
struct listSpec lspec3 = LIST_SPEC( &PrintStrg , const char *, ary3 );
 
void PrintLstSpec(const ListSpec *ls)
{ printf("not-implemented");
}
ListSpec listList[] = { &lspec1, &lspec2, &lspec3 };
struct listSpec llSpec = LIST_SPEC(&PrintLstSpec, ListSpec, listList);
 
int main(int argc, char *argv[])
{
LoopMultiple( &llSpec);
return 0;
}

[edit] C#

class Program
{
static void Main(string[] args)
{
char[] a = { 'a', 'b', 'c' };
char[] b = { 'A', 'B', 'C' };
int[] c = { 1, 2, 3 };
int min = Math.Min(a.Length, b.Length);
min = Math.Min(min, c.Length);
for (int i = 0; i < min; i++)
Console.WriteLine("{0}{1}{2}", a[i], b[i], c[i]);
}
}


Using Zip:

 
int[] numbers = { 1, 2, 3, 4 };
string[] words = { "one", "two", "three" };
Console.WriteLine(numbers.Zip(words, (first, second) => first + " " + second));
 
 

Like a perl programmer would write it:

 
Console.WriteLine((new[] { 1, 2, 3, 4 }).Zip(new[] { "a", "b", "c" }, (f, s) => f + " " + s));
 

[edit] C++

With std::vectors:

#include <iostream>
#include <vector>
 
int main(int argc, char* argv[])
{
std::vector<char> ls(3); ls[0] = 'a'; ls[1] = 'b'; ls[2] = 'c';
std::vector<char> us(3); us[0] = 'A'; us[1] = 'B'; us[2] = 'C';
std::vector<int> ns(3); ns[0] = 1; ns[1] = 2; ns[2] = 3;
 
std::vector<char>::const_iterator lIt = ls.begin();
std::vector<char>::const_iterator uIt = us.begin();
std::vector<int>::const_iterator nIt = ns.begin();
for(; lIt != ls.end() && uIt != us.end() && nIt != ns.end();
++lIt, ++uIt, ++nIt)
{
std::cout << *lIt << *uIt << *nIt << "\n";
}
}

Using static arrays:

#include <iostream>
 
int main(int argc, char* argv[])
{
char ls[] = {'a', 'b', 'c'};
char us[] = {'A', 'B', 'C'};
int ns[] = {1, 2, 3};
 
for(size_t li = 0, ui = 0, ni = 0;
li < sizeof(ls) && ui < sizeof(us) && ni < sizeof(ns) / sizeof(int);
++li, ++ui, ++ni)
{
std::cout << ls[li] << us[ui] << ns[ni] << "\n";
}
}

[edit] Clojure

(doseq [s (map #(str %1 %2 %3) "abc" "ABC" "123")] 
(println s))

The sequence stops when the shortest list is exhausted.

[edit] Common Lisp

[edit] Using functional application

(mapc (lambda (&rest args)
(format t "~{~A~}~%" args))
'(|a| |b| |c|)
'(a b c)
'(1 2 3))

If lists are different lengths, it stops after the shortest one.

[edit] Using LOOP

 
(loop for x in '("a" "b" "c")
for y in '(a b c)
for z in '(1 2 3)
do (format t "~a~a~a~%" x y z))
 

[edit] D

import std.stdio, std.range;
 
void main () {
foreach (a, b, c; zip("abc", "ABC", [1, 2, 3]))
writeln(a, b, c);
}

Output:

aA1
bB2
cC3

zip() allows to specify the stopping policy, on default it stops when the shortest range is exhausted (same as StoppingPolicy.shortest):

import std.stdio, std.range;
 
void main () {
auto a1 = [1, 2];
auto a2 = [1, 2, 3];
alias StoppingPolicy sp;
 
// Stops when the shortest range is exhausted
foreach (p; zip(sp.shortest, a1, a2))
writeln(p.tupleof);
writeln();
 
// Stops when the longest range is exhausted
foreach (p; zip(sp.longest, a1, a2))
writeln(p.tupleof);
writeln();
 
// Requires that all ranges are equal
foreach (p; zip(sp.requireSameLength, a1, a2))
writeln(p.tupleof);
}
Output:
11
22

11
22
03

11
22

Followed by an exception with message "Inequal-length ranges passed to Zip".

There is also std.range.lockstep:

import std.stdio, std.range;
 
void main() {
auto arr1 = [1, 2, 3, 4, 5];
auto arr2 = [6, 7, 8, 9, 10];
 
foreach (ref a, ref b; lockstep(arr1, arr2))
a += b;
 
assert(arr1 == [7, 9, 11, 13, 15]);
 
// Lockstep also supports iteration with an index variable
foreach (index, a, b; lockstep(arr1, arr2))
writefln("Index %s: a = %s, b = %s", index, a, b);
}

Lower level code that stops at the shortest length:

import std.stdio, std.algorithm;
 
void main () {
auto s1 = "abc";
auto s2 = "ABC";
auto a1 = [1, 2];
 
foreach (i; 0 .. min(s1.length, s2.length, a1.length))
writeln(s1[i], s2[i], a1[i]);
}

Output:

aA1
bB2

[edit] Delphi

program LoopOverArrays;
 
{$APPTYPE CONSOLE}
 
uses SysUtils;
 
const
ARRAY1: array [1..3] of string = ('a', 'b', 'c');
ARRAY2: array [1..3] of string = ('A', 'B', 'C');
ARRAY3: array [1..3] of Integer = (1, 2, 3);
var
i: Integer;
begin
for i := 1 to 3 do
Writeln(Format('%s%s%d', [ARRAY1[i], ARRAY2[i], ARRAY3[i]]));
 
Readln;
end.

[edit] DWScript

If the arrays don't have the same bounds, an index out of bound exception will be triggered when attempting to access a non-existing element.

const a1 = ['a', 'b', 'c'];
const a2 = ['A', 'B', 'C'];
const a3 = [1, 2, 3];
 
var i : Integer;
for i := 0 to 2 do
PrintLn(Format('%s%s%d', [a1[i], a2[i], a3[i]]));

[edit] E

E lacks a nice way to do this; this is to be fixed, once we figure out what to do. However, iteration over an List produces its indexes as keys, so a not entirely awful idiom exists:

def a1 := ["a","b","c"]
def a2 := ["A","B","C"]
def a3 := ["1","2","3"]
 
for i => v1 in a1 {
println(v1, a2[i], a3[i])
}

This will obviously fail if a2 or a3 are shorter than a1, and omit items if a2 or a3 are longer.

Given a parallel iteration utility, we might write this:

for [v1, v2, v3] in zip(a1, a2, a3) {
println(v1, v2, v3)
}

zip cannot yet be defined for all collections (other than by iterating over each one and storing the results in a List first); but we can define it for numeric-indexed collections such as Lists, as below. Both a definition for any number of collections and two collections is given; the latter in order to demonstrate the principle without the clutter resulting from handling a variable number of collections.

def zip {
to run(l1, l2) {
def zipped {
to iterate(f) {
for i in int >= 0 {
f(i, [l1.fetch(i, fn { return }),
l2.fetch(i, fn { return })])
}
}
}
return zipped
}
 
match [`run`, lists] {
def zipped {
to iterate(f) {
for i in int >= 0 {
var tuple := []
for l in lists {
tuple with= l.fetch(i, fn { return })
}
f(i, tuple)
}
}
}
zipped
}
}

(This will stop when the end of the shortest collection is reached.)

[edit] Efene

@public 
run = fn () {
lists.foreach(fn ((A, B, C)) { io.format("~s~n", [[A, B, C]]) }, lists.zip3("abc", "ABC", "123"))
}
 

If the lists are not all the same length, an error is thrown.

[edit] Ela

open console list imperative
 
xs = zipWith3 (\x y z -> show x ++ show y ++ show z) ['a','b','c'] ['A','B','C'] [1,2,3]
 
each writen xs

The code above can be written shorter. First there is no need in lists as soon as strings in Ela can be treated as lists. Also instead of explicit labmda one can use partial application and a standard composition operator:

xs = zipWith3 (\x -> (x++) >> (++)) "abc" "ABC" "123"

[edit] Erlang

Shortest option:

lists:zipwith3(fun(A,B,C)-> io:format("~s~n",[[A,B,C]]) end, "abc", "ABC", "123").

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 lists:foreach/2, which also guarantees evaluation order:

lists:foreach(fun({A,B,C}) ->  io:format("~s~n",[[A,B,C]]) end,
lists:zip3("abc", "ABC", "123")).

If the lists are not all the same length, an error is thrown.

[edit] Euphoria

There are many ways to do this. All of them rely on what strings really are. If they are all "strings", it's quite easy:

 
sequence a, b, c
 
a = "abc"
b = "ABC"
c = "123"
 
for i = 1 to length(a) do
puts(1, a[i] & b[i] & c[i] & "\n")
end for
 

If not, and the other sequence is known to contain only integers:

 
sequence a, b, c
 
a = "abc"
b = "ABC"
c = {1, 2, 3}
 
for i = 1 to length(a) do
printf(1, "%s%s%g\n", {a[i], b[i], c[i]})
end for
 

A general solution for any arbitrary strings of characters or numbers can get a bit complex. This is because of how sequences are stored and printed out. One possible answer is as follows, if you know that only alphanumeric characters are used:

 
for i = 1 to length(a) do
if (a[i] >= '0' and a[i] <= '9') then
a[i] -= '0'
end if
if (b[i] >= '0' and b[i] <= '9') then
b[i] -= '0'
end if
if (c[i] >= '0' and c[i] <= '9') then
c[i] -= '0'
end if
printf(1, "%s%s%s\n", {a[i], b[i], c[i]})
end for
 

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 in ASCII.

With all three of the above solutions, if any of the strings are smaller than the first, it will return an error.

[edit] Factor

"abc" "ABC" "123" [ [ write1 ] tri@ nl ] 3each

[edit] Fantom

This will stop when it reaches the end of the shortest list.

 
class LoopMultiple
{
public static Void main ()
{
List arr1 := ["a", "b", "c"]
List arr2 := ["A", "B", "C"]
List arr3 := [1, 2, 3]
[arr1.size, arr2.size, arr3.size].min.times |Int i|
{
echo ("${arr1[i]}${arr2[i]}${arr3[i]}")
}
}
}
 

[edit] Forth

create a  char a , char b , char c ,
create b char A , char B , char C ,
create c char 1 , char 2 , char 3 ,
 
: main
3 0 do cr
a i cells + @ emit
b i cells + @ emit
c i cells + @ emit
loop
cr
a b c
3 0 do cr
3 0 do
rot dup @ emit cell+
loop
loop
drop drop drop
;

[edit] Fortran

program main
implicit none
 
integer,parameter :: n_vals = 3
character(len=*),dimension(n_vals),parameter :: ls = ['a','b','c']
character(len=*),dimension(n_vals),parameter :: us = ['A','B','C']
integer,dimension(n_vals),parameter :: ns = [1,2,3]
 
integer :: i !counter
 
do i=1,n_vals
write(*,'(A1,A1,I1)') ls(i),us(i),ns(i)
end do
 
end program main
 

[edit] F#

for c1,c2,n in Seq.zip3 ['a';'b';'c'] ['A';'B';'C'] [1;2;3] do
printfn "%c%c%d" c1 c2 n

When one sequence is exhausted, any remaining elements in the other sequences are ignored.

[edit] Go

Go's "for each" statement only looks at a single array (or other iterable type.) To access the three in parallel, they have to be explicitly indexed.

If a2 or a3 were shorter, the program would panic with "runtime error: index out of range." If a2 or a3 were longer, extra elements would be ignored. Go philosophy is that you should explicitly check for whatever conditions are meaningful in your application and explicitly handle whatever errors are plausible.

package main
 
import "fmt"
 
var a1 = []int{'a','b','c'}
var a2 = []int{'A','B','C'}
var a3 = []int{1,2,3}
 
func main() {
for i := range a1 {
fmt.Printf("%c%c%d\n", a1[i], a2[i], a3[i])
}
}

[edit] Golfscript

["a" "b" "c"]:a;
["A" "B" "C"]:b;
["1" "2" "3"]:c;
[a b c]zip{puts}/

If there are arrays of different size, the shorter are treated as "null-padded" array.


[edit] Groovy

Solution:

def synchedConcat = { a1, a2, a3 ->
assert a1 && a2 && a3
assert a1.size() == a2.size()
assert a2.size() == a3.size()
[a1, a2, a3].transpose().collect { "${it[0]}${it[1]}${it[2]}" }
}

Test:

def x = ['a', 'b', 'c']
def y = ['A', 'B', 'C']
def z = [1, 2, 3]
 
synchedConcat(x, y, z).each { println it }

Output:

aA1
bB2
cC3

[edit] Haskell

main = mapM_ putStrLn $ zipWith3 (\a b c -> [a,b,c]) "abc" "ABC" "123"

zipWith (2 lists) and zipWith3 are exported by Prelude. zipWith4 through zipWith7 are available in the Data.List module.

If lists are different lengths, it stops after the shortest one.

[edit] Haxe

using Lambda;
using Std;
 
class Main
{
 
static function main()
{
var a = ['a', 'b', 'c'];
var b = ['A', 'B', 'C'];
var c = [1, 2, 3];
 
//Find smallest array
var len = [a, b, c]
.map(function(a) return a.length)
.fold(Math.min, 0x0FFFFFFF)
.int();
 
for (i in 0...len)
Sys.println(a[i] + b[i] + c[i].string());
}
}

[edit] HicEst

CHARACTER :: A = "abc"
REAL :: C(3)
 
C = $ ! 1, 2, 3
 
DO i = 1, 3
WRITE() A(i), "ABC"(i), C(i)
ENDDO

[edit] Icon and Unicon

The first solution uses co-expressions to produce parallel evaluation.

procedure main()
a := create !["a","b","c"]
b := create !["A","B","C"]
c := create !["1","2","3"]
while write(@a,@b,@c)
end

The second solution is more like other procedural languages and also handles unequal list lengths.

link numbers  # for max
 
procedure main()
 
a := ["a","b","c"]
b := ["A","B","C","D"]
c := [1,2,3]
 
every i := 1 to max(*a,*b,*c) do
write(a[i]|"","\t",b[i]|"","\t",c[i]|"")
end


Uses max from numbers

[edit] J

For arrays of different types:

,.&:(":"0@>)/ 'abc' ; 'ABC' ; 1 2 3

This approach works by representing the digits as characters.

Where arrays are all the same type (all numeric or all string):

,.&:>/ 'abc' ; 'ABC' ; '123'

Both of these implementations reject arrays with conflicting lengths.

Other options include:

|: 'abc', 'ABC' ,:;":&> 1 2 3
|: 'abc', 'ABC',: '123'

These implementations pad short arrays with spaces.

Or:

|:>]&.>L:_1 'abc';'ABC';<1 2 3

This implementation puts each item from each of the original lists into a box and forms an array of boxes. (A "box" is a immutable pointer to immutable data -- in other words value semantics instead of reference semantics -- and "putting an item into a box" is obtaining one of these pointers for that item.) This implementation extends any short array by providing empty boxes to represent the missing elements.

[edit] Java

Translation of: JavaScript
String[] a = {"a","b","c"};
String[] b = {"A","B","C"};
int[] c = {1,2,3};
for (int i = 0;i < a.length;i++) {
System.out.println(a[i] + b[i] + c[i] + "\n");
}

If the first array is too short, it will stop when it gets to the end of the first array. If one of the other arrays is too short, an ArrayIndexOutOfBoundException will be thrown.


[edit] K

{,/$x}'+("abc";"ABC";1 2 3)

Output:

 
("aA1"
"bB2"
"cC3")
 

If the length of the arrays are different, then K croaks with "length error".

The following is a more general approach where

 
&/#:'x
 

calculates the minimum length of the arrays and is used to index the first elements in each array.

 
{+x[;!(&/#:'x)]}("abc";"ABC";"1234")
 

Output:

 
("aA1"
"bB2"
"cC3")
 

If the arrays are of different type then the arrays must be convert to strings.

 
{a:,/'($:'x);+a[;!(&/#:'a)]}("abc";"ABC";1 2 3 4)
 

[edit] JavaScript

This loops over the indices of the first array, and uses that to index into the others.

var a = ["a","b","c"],
b = ["A","B","C"],
c = [1,2,3],
output = "",
i;
for (i = 0; i < a.length; i += 1) {
output += a[i] + b[i] + c[i] + "\n";
}

If the b or c arrays are too "short", you will see the string "undefined" appear in the output.

[edit] Liberty BASIC

a$(1)="a" : a$(2)="b" : a$(3)="c"
b$(1)="A" : b$(2)="B" : b$(3)="C"
c(1)=1 : c(2)=2 : c(3)=3
 
 
for i = 1 to 3
print a$(i);b$(i);c(i)
next

[edit] Lisaac

Section Header
 
+ name := ARRAY_LOOP_TEST;
 
Section Public
 
- main <- (
+ a1, a2 : ARRAY[CHARACTER];
+ a3 : ARRAY[INTEGER];
 
a1 := ARRAY[CHARACTER].create 1 to 3;
a2 := ARRAY[CHARACTER].create 1 to 3;
a3 := ARRAY[INTEGER].create 1 to 3;
 
1.to 3 do { i : INTEGER;
a1.put ((i - 1 + 'a'.code).to_character) to i;
a2.put ((i - 1 + 'A'.code).to_character) to i;
a3.put i to i;
};
 
1.to 3 do { i : INTEGER;
a1.item(i).print;
a2.item(i).print;
a3.item(i).print;
'\n'.print;
};
);

[edit]

Works with: UCB Logo
show (map [(word ?1 ?2 ?3)] [a b c] [A B C] [1 2 3])    ; [aA1 bB2 cC3]
 
(foreach [a b c] [A B C] [1 2 3] [print (word ?1 ?2 ?3)])  ; as above, one per line

[edit] Lua

This can be done with a simple for loop:

 
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
 

but it may be more enlightening (and in line with the spirit of the challenge) to use the generic for:

 
function iter(a, b, c)
local i = 0
return function()
i = i + 1
return a[i], b[i], c[i]
end
end
 
for u, v, w in iter(a1, a2, a3) do print(u, v, w) end
 

[edit] Mathematica

This can be done with a built-in function:

 
MapThread[Print, {{"a", "b", "c"}, {"A", "B", "C"}, {1, 2, 3}}];
 

All arguments must be lists of the same length.

[edit] Mercury

 
:- module multi_array_loop.
:- interface.
 
:- import_module io.
:- pred main(io::di, io::uo) is det.
 
:- implementation.
:- import_module char, list, string.
 
main(!IO) :-
A = ['a', 'b', 'c'],
B = ['A', 'B', 'C'],
C = [1, 2, 3],
list.foldl_corresponding3(print_elems, A, B, C, !IO).
 
:- pred print_elems(char::in, char::in, int::in, io::di, io::uo) is det.
 
print_elems(A, B, C, !IO) :-
io.format("%c%c%i\n", [c(A), c(B), i(C)], !IO).
 

The foldl_corresponding family of procedures all throw a software_error/1 exception if the lengths of the lists are not the same.

[edit] Modula-3

MODULE MultiArray EXPORTS Main;
 
IMPORT IO, Fmt;
 
TYPE ArrIdx = [1..3];
 
VAR
arr1 := ARRAY ArrIdx OF CHAR {'a', 'b', 'c'};
arr2 := ARRAY ArrIdx OF CHAR {'A', 'B', 'C'};
arr3 := ARRAY ArrIdx OF INTEGER {1, 2, 3};
 
BEGIN
FOR i := FIRST(ArrIdx) TO LAST(ArrIdx) DO
IO.Put(Fmt.Char(arr1[i]) & Fmt.Char(arr2[i]) & Fmt.Int(arr3[i]) & "\n");
END;
END MultiArray.

[edit] MUMPS

Pieces of String version

 
LOOPMULT
N A,B,C,D,%
S A="a,b,c,d"
S B="A,B,C,D"
S C="1,2,3"
S D=","
F %=1:1:$L(A,",") W !,$P(A,D,%),$P(B,D,%),$P(C,D,%)
K A,B,C,D,%
Q
 

When there aren't enough elements, a null string will be returned from the $Piece function.

USER>d LOOPMULT^ROSETTA
 
aA1
bB2
cC3
dD

Local arrays version

 
LOOPMULU
N A,B,C,D,%
S A(1)="a",A(2)="b",A(3)="c",A(4)="d"
S B(1)="A",B(2)="B",B(3)="C",B(4)="D"
S C(1)="1",C(2)="2",C(3)="3"
 ; will error S %=$O(A("")) F Q:%="" W !,A(%),B(%),C(%) S %=$O(A(%))
S %=$O(A("")) F Q:%="" W !,$G(A(%)),$G(B(%)),$G(C(%)) S %=$O(A(%))
K A,B,C,D,%
 

The commented out line will throw an <UNDEFINED> error when trying to look up D(4). Using the $Get function as a wrapper means that if the subscript for the array doesn't exist, a null string will be returned. This same syntax is used for globals (permanent variables, that have a caret "^" as the first character).

USER>D LOOPMULU^ROSETTA
 
aA1
bB2
cC3
dD
USER>D LOOPMULV^ROSETTA

aA1
bB2
cC3
dD
 S %=$O(A("")) F  Q:%=""  W !,A(%),B(%),C(%) S %=$O(A(%))
                                        ^
<UNDEFINED>LOOPMULV+5^ROSETTA *C(4)

[edit] Nemerle

It "feels" better to use zip() for this, unfortunately the built in zip() only takes two lists.

using System;
using System.Console;
 
module LoopMultiple
{
Zip3[T1, T2, T3] (x : list[T1], y : list[T2], z : list[T3]) : list[T1 * T2 * T3]
{
|(x::xs, y::ys, z::zs) => (x, y, z)::Zip3(xs, ys, zs)
|([], [], []) => []
|(_, _, []) => throw ArgumentNullException()
|(_, [], _) => throw ArgumentNullException()
|([], _, _) => throw ArgumentNullException()
}
 
Main() : void
{
def first = ['a', 'b', 'c'];
def second = ["A", "B", "C"];
def third = [1, 2, 3];
 
foreach ((x, y, z) in Zip3(first, second, third))
WriteLine($"$x$y$z");
}
}
Alternately:
Translation of: C#
using System.Console;
 
module LoopMult
{
Main() : void
{
def first = array['a', 'b', 'c'];
def second = array['A', 'B', 'C'];
def third = array[1, 2, 3];
 
when (first.Length == second.Length && second.Length == third.Length)
foreach (i in [0 .. (first.Length - 1)])
WriteLine("{0}{1}{2}", first[i], second[i], third[i]);
}
}

[edit] NetRexx

/* NetRexx */
options replace format comments java crossref savelog symbols nobinary
 
say 'Using arrays'
aa = ['a', 'b', 'c', 'd']
bb = ['A', 'B', 'C']
cc = [1, 2, 3, 4]
 
loop x_ = 0 for aa.length
do
ax = aa[x_]
catch ArrayIndexOutOfBoundsException
ax = ' '
end
do
bx = bb[x_]
catch ArrayIndexOutOfBoundsException
bx = ' '
end
do
cx = cc[x_]
catch ArrayIndexOutOfBoundsException
cx = ' '
end
 
say ax || bx || cx
end x_
 
say 'Using indexed strings (associative arrays)'
ai = sampleData('a b c d')
bi = sampleData('A B C')
ci = sampleData('1 2 3 4')
 
loop x_ = 1 to ai[0]
say ai[x_] || bi[x_] || ci[x_]
end x_
 
method sampleData(arg) public static returns Rexx
smp = ' '
smp[0] = arg.words
loop i_ = 1 to smp[0]
smp[i_] = arg.word(i_)
end i_
 
return smp
 

Output:

Using arrays
aA1
bB2
cC3
d 4
Using indexed strings (associative arrays)
aA1
bB2
cC3
d 4

[edit] Objeck

 
class MultipleArrayAccess {
function : Main(args : String[]) ~ Nil {
a := ["a", "b", "c"];
b := ["A", "B", "C"];
c := [1, 2, 3];
 
each(i : a) {
a[i]->Append(b[i]);
a[i]->Append(c[i]);
a[i]->PrintLine();
};
}
}
 

If the arrays are different lengths then an out-of-bounds error will be raised.

[edit] OCaml

an immediate solution:

let a1 = [| 'a'; 'b'; 'c' |]
and a2 = [| 'A'; 'B'; 'C' |]
and a3 = [| '1'; '2'; '3' |] ;;
 
Array.iteri (fun i c1 ->
print_char c1;
print_char a2.(i);
print_char a3.(i);
print_newline()
) a1 ;;

a more generic solution could be to use a function which iterates over a list of arrays:

let n_arrays_iter ~f = function
| [] -> ()
| x::xs as al ->
let len = Array.length x in
let b = List.for_all (fun a -> Array.length a = len) xs in
if not b then invalid_arg "n_arrays_iter: arrays of different length";
for i = 0 to pred len do
let ai = List.map (fun a -> a.(i)) al in
f ai
done

this function raises Invalid_argument exception if arrays have different length, and has this signature:

val n_arrays_iter : f:('a list -> unit) -> 'a array list -> unit

how to use it with arrays a1, a2 and a3 defined before:

let () =
n_arrays_iter [a1; a2; a3] ~f:(fun l ->
List.iter print_char l;
print_newline());
;;

[edit] ooRexx

 
x = .array~of("a", "b", "c")
y = .array~of("A", "B", "C")
z = .array~of(1, 2, 3)
 
loop i = 1 to x~size
say x[i]y[i]z[i]
end
 


[edit] Oz

for
I in [a b c]
J in ['A' 'B' 'C']
K in [1 2 3]
do
{System.showInfo I#J#K}
end

The loop will stop when the shortest list is exhausted.

[edit] PARI/GP

This version stops when the shortest vector is exhausted.

loopMultiple(V)={
my(l=#V[1]);
for(i=2,#V,l=min(l,#V[i]));
for(i=1,#V[1],
for(j=1,#V,
print1(V[j][i])
);
print()
)
};

This version prints blanks when a vector is exhausted.

loopMultiple(V)={
my(l=0);
for(i=1,#V,l=max(l,#V[i]));
for(i=1,#V[1],
for(j=1,#V,
if(#V[j]<i,
print1(" ")
,
print1(V[j][i])
)
);
print()
)
};

[edit] Pascal

See Delphi

[edit] Perl

sub zip (&@)
{
my $code = shift;
my $min;
$min = $min && $#$_ > $min ? $min : $#$_ for @_;
 
for my $i(0..$min){ $code->(map $_->[$i] ,@_) }
}
my @a1 = qw( a b c );
my @a2 = qw( A B C );
my @a3 = qw( 1 2 3 );
 
zip { print @_,"\n" }\(@a1, @a2, @a3);

This implementation will stop producing items when the shortest array ends.

[edit] Perl 6

Works with: niecza version 2011.07
for <a b c> Z <A B C> Z 1, 2, 3 -> $x, $y, $z {
say $x, $y, $z;
}

The Z 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.

Note that we can also factor out the concatenation by making the Z metaoperator apply the ~ concatenation operator across each triple:

for <a b c> Z~ <A B C> Z~ 1, 2, 3 -> $line {
say $line;
}

We could also use the zip-to-string with the reduction metaoperator:

.say for [Z~] [<a b c>], [<A B C>], [1,2,3]

[edit] PHP

$a = array('a', 'b', 'c');
$b = array('A', 'B', 'C');
$c = array('1', '2', '3'); //These don't *have* to be strings, but it saves PHP from casting them later
 
if ((sizeOf($a) !== sizeOf($b)) || (sizeOf($b) !== sizeOf($c))){
throw new Exception('All three arrays must be the same length');
}
foreach ($a as $key => $value){
echo "{$a[$key]}{$b[$key]}{$c[$key]}\n";
}

This implementation throws an exception if the arrays are not all the same length.

[edit] PicoLisp

(mapc prinl
'(a b c)
'(A B C)
(1 2 3) )

The length of the first argument list controls the operation. If subsequent lists are longer, their remaining values are ignored. If they are shorter, NIL is passed to the function.

[edit] PL/I

 
declare P(3) character (1) initial ('a', b', 'c'),
Q(3) character (1) initial ('A', 'B', 'C'),
R(3) fixed decimal (1) initial (1, 2, 3);
 
do i = lbound(P,1) to hbound(P,1);
put skip edit (P(i), Q(i), R(i)) (2 A, F(1));
end;
 

[edit] PostScript

Library: initlib
 
% transpose is defined in initlib like this.
/transpose {
[ exch {
{ {empty? exch pop} map all?} {pop exit} ift
[ exch {} {uncons {exch cons} dip exch} fold counttomark 1 roll] uncons
} loop ] {reverse} map
}.
 
% using it.
[[/a /b /c] [/A /B /C] [1 2 3]] transpose
 

[edit] PowerBASIC

FUNCTION PBMAIN () AS LONG
DIM x(2), y(2) AS STRING * 1
DIM z(2) AS LONG
 
'data
ARRAY ASSIGN x() = ("a", "b", "c")
ARRAY ASSIGN y() = ("A", "B", "C")
ARRAY ASSIGN z() = (1, 2, 3)
 
'set upper bound
C& = UBOUND(x)
IF UBOUND(y) > C& THEN C& = UBOUND(y)
IF UBOUND(z) > C& THEN C& = UBOUND(z)
 
OPEN "output.txt" FOR OUTPUT AS 1
FOR L& = 0 TO C&
IF L& <= UBOUND(x) THEN PRINT #1, x(L&);
IF L& <= UBOUND(y) THEN PRINT #1, y(L&);
IF L& <= UBOUND(z) THEN PRINT #1, TRIM$(STR$(z(L&)));
PRINT #1,
NEXT
CLOSE
END FUNCTION

[edit] Prolog

Works with SWI-Prolog

multiple_arrays(L1, L2, L3) :-
maplist(display, L1, L2, L3).
 
display(A,B,C) :-
writef('%s%s%s\n', [[A],[B],[C]]).
 

Output examples :

 ?- multiple_arrays("abc", "ABC", "123").
aA1
bB2
cC3
true.

 ?- multiple_arrays("abc", "AB", "123").
aA1
bB2
false.

[edit] PureBasic

OpenConsole()
; Fill arrays
Dim a.s(2)
Dim b.s(2)
Dim c(2)
For Arrayposition = 0 To ArraySize(a())
a(Arrayposition) = Chr(Asc("a") + Arrayposition)
b(Arrayposition) = Chr(Asc("A") + Arrayposition)
c(Arrayposition) = Arrayposition + 1
Next
; loop over them
For Arrayposition = 0 To ArraySize(a())
PrintN(a(Arrayposition) + b(Arrayposition) + Str(c(Arrayposition)))
Next
Input() ;wait for Enter before ending

If they have different lengths there are two cases:
a() is the shortest one: Only elements up to maximum index of a() are printed
a() is bigger than another one: if exceeding index to much, program crashes,
else it may work because there is some "free space" after end of assigned array memory.
For example if a has size 4, line dD4 will also be printed. size 20 leads to an crash
This is because ReDim becomes slow if everytime there is a change to array size new memory has to be allocated.

[edit] Python

Using zip():

>>> print ( '\n'.join(''.join(x) for x in zip('abc', 'ABC', '123')) )
aA1
bB2
cC3
>>>

If lists are different lengths, zip() stops after the shortest one.

Using map():

>>> print ( '\n'.join(map(lambda *x: ''.join(x), 'abc', 'ABC', '123')) )
aA1
bB2
cC3
>>>

If lists are different lengths, map() in Python 2.x pretends that the shorter lists were extended with None items; map() in Python 3.x stops after the shortest one.

Using itertools.imap() (Python 2.x):

from itertools import imap
 
def join3(a,b,c):
print a+b+c
 
imap(join3,'abc','ABC','123')

If lists are differnt lengths, imap() stops after the shortest is exhausted.

[edit] zip_longest

Python 3.X has zip_longest which fills shorter iterables with its fillvalue argument which defaults to None (similar to the behavior of map() in Python 2.x):

>>> from itertools import zip_longest
>>> print ( '\n'.join(''.join(x) for x in zip_longest('abc', 'ABCD', '12345', fillvalue='#')) )
aA1
bB2
cC3
#D4
##5
>>>

(The Python 2.X equivalent is itertools.izip_longest)

[edit] R

multiloop <- function(...)
{
# Retrieve inputs and convert to a list of character strings
arguments <- lapply(list(...), as.character)
 
# Get length of each input
lengths <- sapply(arguments, length)
 
# Loop over elements
for(i in seq_len(max(lengths)))
{
# Loop over inputs
for(j in seq_len(nargs()))
{
# print a value or a space (if that input has finished)
cat(ifelse(i <= lengths[j], arguments[[j]][i], " "))
}
cat("\n")
}
}
multiloop(letters[1:3], LETTERS[1:3], 1:3)

Same thing as a single function call. But throws error if the arrays differ in length.

 
apply(data.frame(letters[1:3], LETTERS[1:3], 1:3), 1,
function(row) { cat(row, "\n", sep='') })
 

[edit] Racket

Racket for loops can loop over an arbitrary number of sequences at once:

 
#lang racket
 
(for ([i-1 '(a b c)]
[i-2 '(A B C)]
[i-3 '(1 2 3)])
(printf "~a ~a ~a~n" i-1 i-2 i-3))
 

[edit] REXX

[edit] same size arrays

If any of the array's elements are missing or it is a short list, a blank is substituted to retain visual fidelity in the output.

When all elements are blank, then it signifies the end of the arrays.

/*REXX program shows how to  simultaneously  loop over  multiple arrays.*/
 
x. = ' '; x.1 = "a"; x.2 = 'b'; x.3 = "c"
y. = ' '; y.1 = "A"; y.2 = 'B'; y.3 = "C"
z. = ' '; z.1 = "1"; z.2 = '2'; z.3 = "3"
 
do j=1 until output=''
output=x.j || y.j || z.j
say output
end /*j*/
/*stick a fork in it, we're done.*/

output

aA1
bB2
cC3

[edit] dissimilar sized arrays

In this example, two of the arrays are extended (past the 1st example).
Also note that REXX doesn't require quotes around numbers (they're optional).

/*REXX program shows how to  simultaneously  loop over  multiple arrays.*/
 
x.=' '; x.1="a"; x.2='b'; x.3="c"; x.4='d'
y.=' '; y.1="A"; y.2='B'; y.3="C";
z.=' '; z.1= 1 ; z.2= 2 ; z.3= 3 ; z.4= 4; z.5= 5
 
do j=1 until output=''
output= x.j || y.j || z.j
say output
end /*j*/
/*stick a fork in it, we're done.*/

output

aA1
bB2
cC3
d 4
  5

[edit] Ruby

['a','b','c'].zip(['A','B','C'], [1,2,3]) {|i,j,k| puts "#{i}#{j}#{k}"}

or

['a','b','c'].zip(['A','B','C'], [1,2,3]) {|a| puts a.join('')}

Both of these loops print aA1, bB2, cC3.

Array#zip iterates once for each element of the receiver. If an argument array is longer, the excess elements are ignored. If an argument array is shorter, the value nil is supplied.


[edit] Run BASIC

for i = 1 to 3
a$(i) = chr$(i+96)
b$(i) = chr$(i+64)
c(i) = i
next i
 
for i = 1 to 3
print a$(i);b$(i);c(i)
next

[edit] Salmon

// First, we'll define a general-purpose zip() to zip any
// number of lists together.
function zip(...)
{
variable result;
variable list_num := 0;
iterate(arg; arguments)
{
variable elem_num := 0;
iterate (x; arg)
{
result[elem_num][list_num] := x;
++elem_num;
};
++list_num;
};
return result;
};
 
immutable a := ["a", "b", "c"],
b := ["A", "B", "C"],
c := [1, 2, 3];
iterate (x; zip(a, b, c))
print(x[0], x[1], x[2], "\n");;

The preceding code will throw an exception if the lists aren't the same length. Here's an example that will print a number of lines equal to the length of the longest list and print nothing for elements that are missing if some lists are shorter than the longest:

// First, we'll define a general-purpose zip() to zip any
// number of lists together.
function zip(...)
{
variable result := [];
variable list_num := 0;
iterate(arg; arguments)
{
variable elem_num := 0;
iterate (x; arg)
{
if (elem_num >= length(result))
result[elem_num] := <<(* --> "")>>;;
result[elem_num][list_num] := x;
++elem_num;
};
++list_num;
};
return result;
};
 
immutable a := ["a", "b", "c"],
b := ["A", "B", "C"],
c := [1, 2, 3];
iterate (x; zip(a, b, c))
print(x[0], x[1], x[2], "\n");;

[edit] Sather

class MAIN is
main is
a :ARRAY{STR} := |"a", "b", "c"|;
b :ARRAY{STR} := |"A", "B", "C"|;
c :ARRAY{STR} := |"1", "2", "3"|;
loop i ::= 0.upto!(2);
#OUT + a[i] + b[i] + c[i] + "\n";
end;
end;
end;

If the index i is out of bounds, a runtime error is raised.

[edit] Scala

 
("abc", "ABC", "123").zipped foreach { (x, y, z) =>
println(x.toString + y + z)
}
 

[edit] Scheme

Scheme provides for-each and map to iterate a function over one or more lists. The map form is used to collect the results into a new list.

 
(let ((a '("a" "b" "c"))
(b '("A" "B" "C"))
(c '(1 2 3)))
(for-each
(lambda (i1 i2 i3)
(display i1)
(display i2)
(display i3)
(newline))
a b c))
 

Scheme has a vector datatype with constant-time retrieval of items held in an ordered sequence. Use srfi-43 to get similar iterators for vectors, vector-for-each and vector-map:

 
(let ((a (vector "a" "b" "c"))
(b (vector "A" "B" "C"))
(c (vector 1 2 3)))
(vector-for-each
(lambda (current-index i1 i2 i3)
(display i1)
(display i2)
(display i3)
(newline))
a b c))
 

Note, the lists or vectors must all be of the same length.

[edit] Standard ML

The below code will combine arbitrarily many lists of strings into a single list with length equal to that of the shortest list.

 
(*
* val combine_lists : string list list -> string list
*)
fun combine_lists nil = nil
| combine_lists (l1::ls) = List.foldl (ListPair.map (fn (x,y) => y ^ x)) l1 ls;
 
(* ["a1Ax","b2By","c3Cz"] *)
combine_lists[["a","b","c"],["1","2","3"],["A","B","C"],["x","y","z"]];
 

[edit] Smalltalk

Works with: GNU Smalltalk
|a b c|
a := OrderedCollection new addAll: #('a' 'b' 'c').
b := OrderedCollection new addAll: #('A' 'B' 'C').
c := OrderedCollection new addAll: #(1 2 3).
 
1 to: (a size) do: [ :i |
(a at: i) display.
(b at: i) display.
(c at: i) displayNl.
].

If index i is out of bound, a runtime error is raised.

[edit] SuperCollider

([\a,\b,\c]+++[\A,\B,\C]+++[1,2,3]).do({|array| array.do(_.post); "".postln })

[edit] Tcl

set list1 {a b c}
set list2 {A B C}
set list3 {1 2 3}
foreach i $list1 j $list2 k $list3 {
puts "$i$j$k"
}

If lists are different lengths, the manual [1] says: "The total number of loop iterations is large enough to use up all the values from all the value lists. If a value list does not contain enough elements for each of its loop variables in each iteration, empty values are used for the missing elements."

[edit] TorqueScript

 
$var[0] = "a b c"
$var[1] = "A B C";
$var[2] = "1 2 3";
 
for(%i=0;%i<3;%i++)
echo(getWord($var[0],%i) @ getWord($var[1],%i) @ getWord($var[2],%i));
 

[edit] TUSCRIPT

 
$$ MODE TUSCRIPT
arr1="a'b'c"
arr2="a'b'C"
arr3="1'2'3"
LOOP a=arr1,b=arr2,c=arr3
PRINT a,b,c
ENDLOOP
 

Output:

aa1
bb2
cC3

[edit] TXR

$ txr -c '@(bind a ("a" "b" "c"))
@(bind b ("A" "B" "C"))
@(bind c ("1" "2" "3"))
@(output)
@ (repeat)
@a@b@c
@ (end)
@(end)'

aA1
bB2
cC3

[edit] UNIX Shell

With the Bourne shell, its for loop (from Loops/Foreach#UNIX Shell) can iterate only one list. We use an index i to access the other lists: set -- $list loads the positional parameters, and shift $i moves our element to $1.

Works with: Bourne Shell
a=a:b:c
b=A:B:C
c=1:2:3
 
oldifs=$IFS
IFS=:
i=0
for wa in $a; do
set -- $b; shift $i; wb=$1
set -- $c; shift $i; wc=$1
 
printf '%s%s%s\n' $wa $wb $wc
 
i=`expr $i + 1`
done
IFS=$oldifs

When the lists have different lengths, this code uses the length of list a. Longer lists ignore their extra elements, and shorter lists give extra empty strings.


Some shells have real arrays, so the iteration is much more simple and easy.

Works with: bash
Works with: ksh93
a=(a b c)
b=(A B C)
c=(1 2 3)
for ((i = 0; i < ${#a[@]}; i++)); do
echo "${a[$i]}${b[$i]}${c[$i]}"
done
Works with: ksh93
Works with: pdksh
set -A a a b c
set -A b A B C
set -A c 1 2 3
((i = 0))
while ((i < ${#a[@]})); do
echo "${a[$i]}${b[$i]}${c[$i]}"
((i++))
done
Works with: zsh
a=(a b c)
b=(A B C)
c=(1 2 3)
for ((i = 1; i <= $#a; i++)); do
echo "$a[$i]$b[$i]$c[$i]"
done

[edit] C Shell

Uses the length of array a. Longer arrays ignore their extra elements, but shorter arrays force the shell to exit with an error like b: Subscript out of range.

set a=(a b c)
set b=(A B C)
set c=(1 2 3)
@ i = 1
while ( $i <= $#a )
echo "$a[$i]$b[$i]$c[$i]"
@ i += 1
end

[edit] Ursala

Compute the transpose of the list formed of the three lists. If they're of unequal lengths, an exception occurs.

#show+
 
main = ~&K7 <'abc','ABC','123'>

output:

aA1
bB2
cC3

[edit] XPL0

string 0;               \use zero terminated strings
include c:\cxpl\codes; \intrinsic 'code' declarations
char A1, A2;
int A3, I;
[A1:= "abc";
A2:= "ABC";
A3:= [1,2,3];
for I:= 0 to 2 do
[ChOut(0, A1(I));
ChOut(0, A2(I));
IntOut(0, A3(I));
CrLf(0);
];
]

Output:

aA1
bB2
cC3

[edit] ZX Spectrum Basic

10 LET sza = 3: REM size of a
20 LET szb = 3: REM size of b
30 LET szc = 3: REM size of c
40 DIM a$(sza): DIM b$(szb): DIM c$(szc)
50 LET max = sza: REM assume a is the biggest
60 IF szb > max THEN LET max = szb: REM now try b
70 IF szc > max THEN LET max = szc: REM or c
80 REM populate our arrays, and as a bonus we already have our demo loop
90 REM we might as well print as we populate showing the arrays in columns
100 FOR l = 1 TO max
110 IF l <= sza THEN READ a$(l): PRINT a$(l);
120 IF l <= szb THEN READ b$(l): PRINT b$(l);
130 IF l <= szc THEN READ c$(l): PRINT c$(l);
140 PRINT: REM newline
150 NEXT l
150 PRINT "The arrays are shown in columns."
160 PRINT "A$ runs down the left hand side,"
170 PRINT "and C$ runs down the right."
180 STOP
200 DATA "a","b","c","A","B","C","1","2","3"

Personal tools
Namespaces

Variants
Actions
Community
Explore
Misc
Toolbox