Loop over multiple arrays simultaneously: Difference between revisions
Content added Content deleted
m (→{{header|Ruby}}: another method) |
|||
Line 26: | Line 26: | ||
=={{header|Ruby}}== |
=={{header|Ruby}}== |
||
<lang ruby>['a','b','c'].zip(['A','B','C'], [1,2,3]).each {|i,j,k| puts "#{i}#{j}#{k}"}</lang> |
<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> |
|||
=={{header|Tcl}}== |
=={{header|Tcl}}== |
Revision as of 16:23, 6 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 ...) 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.
Example, loop over the arrays (a,b,c)
, (A,B,C)
and (1,2,3)
to produce the output
aA1 bB2 cC3
Common Lisp
<lang lisp>(mapc (lambda (&rest args)
(format t "~{~A~}~%" args)) '(|a| |b| |c|) '(a b c) '(1 2 3))</lang>
J
(,.":"0)&:>/ 'abc' ; 'ABC' ; 1 2 3
Python
<lang python>>>> print ( '\n'.join(.join(x) for x in zip('abc', 'ABC', '123')) ) aA1 bB2 cC3 >>> </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>
Tcl
<lang tcl>foreach i {a b c} j {A B C} k {1 2 3} {
puts "$i$j$k"
}</lang>