Loop over multiple arrays simultaneously: Difference between revisions

Content added Content deleted
(→‎{{header|D}}: add implementation)
(added PowerBASIC (and rephrased task description a bit))
Line 1: Line 1:
{{task|Iteration}}Loop over multiple arrays (or lists or tuples ...) and print the ''i''th 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.
{{task|Iteration}}Loop over multiple arrays (or lists or tuples or whatever they're called in your language) and print the ''i''th 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 <code>(a,b,c)</code>, <code>(A,B,C)</code> and <code>(1,2,3)</code> to produce the output
For this example, loop over the arrays <code>(a,b,c)</code>, <code>(A,B,C)</code> and <code>(1,2,3)</code> to produce the output
<pre>aA1
<pre>aA1
bB2
bB2
Line 140: Line 140:
print_newline()
print_newline()
) a1 ;;</lang>
) 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}}==
=={{header|Python}}==