Loop over multiple arrays simultaneously: Difference between revisions
→{{header|D}}: add implementation |
added PowerBASIC (and rephrased task description a bit) |
||
Line 1:
{{task|Iteration}}Loop over multiple arrays (or lists or tuples
<pre>aA1
bB2
Line 140:
print_newline()
) a1 ;;</lang>
=={{header|PowerBASIC}}==
<lang 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
</lang>
=={{header|Python}}==
|
Revision as of 23:06, 24 August 2009
![Task](http://static.miraheze.org/rosettacodewiki/thumb/b/ba/Rcode-button-task-crushed.png/64px-Rcode-button-task-crushed.png)
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.
Ada
<lang 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; </lang>
Common Lisp
<lang lisp>(mapc (lambda (&rest args)
(format t "~{~A~}~%" args)) '(|a| |b| |c|) '(a b c) '(1 2 3))</lang>
If lists are different lengths, it stops after the shortest one.
D
This code snippet will clamp to the array of the shortest length <lang d> char[]arr1 = "abc",arr2 = "ABC"; int[]arr3 = [1,2,3]; foreach(i,ele;arr1) {
if (i == arr2.length || i == arr3.length) { break; } std.writefln("%s",""~ele~arr2[i]~std.string.toString(arr3[i]));
} </lang>
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:
<lang e>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])
}</lang>
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:
<lang e>for [v1, v2, v3] in zip(a1, a2, a3) {
println(v1, v2, v3)
}</lang>
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.
<lang e>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 }
}</lang>
(This will stop when the end of the shortest collection is reached.)
Erlang
Shortest option: <lang erlang> lists:zipwith3(fun(A,B,C)-> io:format("~s~n",A,B,C) end, "abc", "ABC", "123"). </lang> 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: <lang erlang> lists:foreach(fun({A,B,C}) -> io:format("~s~n",A,B,C) end,
lists:zip3("abc", "ABC", "123")).
</lang> If the lists are not all the same length, an error is thrown.
Haskell
<lang haskell>main = mapM_ putStrLn $ zipWith3 (\a b c -> [a,b,c]) "abc" "ABC" "123"</lang> 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.
J
(,.":"0)&:>/ 'abc' ; 'ABC' ; 1 2 3
Logo
<lang 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 </lang>
OCaml
<lang ocaml>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 ;;</lang>
PowerBASIC
<lang 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 </lang>
Python
Using zip(): <lang python>>>> print ( '\n'.join(.join(x) for x in zip('abc', 'ABC', '123')) ) aA1 bB2 cC3 >>> </lang> If lists are different lengths, zip() stops after the shortest one.
Using map(): <lang python>>>> print ( '\n'.join(map(lambda *x: .join(x), 'abc', 'ABC', '123')) ) aA1 bB2 cC3 >>> </lang> If lists are different lengths, map() pretends that the shorter lists were extended with None items.
R
<lang 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], argumentsj[i], " ")) } cat("\n") }
} multiloop(letters[1:3], LETTERS[1:3], 1:3) </lang>
Ruby
<lang ruby>['a','b','c'].zip(['A','B','C'], [1,2,3]).each {|i,j,k| puts "#{i}#{j}#{k}"}</lang>
or
<lang ruby>['a','b','c'].zip(['A','B','C'], [1,2,3]).each {|a| puts a.join()}</lang>
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.
Tcl
<lang tcl>foreach i {a b c} j {A B C} k {1 2 3} {
puts "$i$j$k"
}</lang> 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."
Ursala
Compute the transpose of the list formed of the three lists. If they're of unequal lengths, an exception occurs. <lang Ursala>
- show+
main = ~&K7 <'abc','ABC','123'> </lang> output:
aA1 bB2 cC3