Loop over multiple arrays simultaneously: Difference between revisions

Grouping BASIC dialects
(GDScript)
(Grouping BASIC dialects)
Line 740:
PRINT array1$(index%) ; array2$(index%) ; array3%(index%)
NEXT</syntaxhighlight>
 
==={{header|FreeBASIC}}===
<syntaxhighlight lang="freebasic">' FB 1.05.0 Win64
 
Function min(x As Integer, y As Integer) As Integer
Return IIf(x < y, x, y)
End Function
 
Dim arr1(1 To 3) As String = {"a", "b", "c"}
Dim arr2(1 To 3) As String = {"A", "B", "C"}
Dim arr3(1 To 3) As Integer = {1, 2, 3}
 
For i As Integer = 1 To 3
Print arr1(i) & arr2(i) & arr3(i)
Next
 
Print
 
' For arrays of different lengths we would need to iterate up to the mimimm length of all 3 in order
' to get a contribution from each one. For example:
 
Dim arr4(1 To 4) As String = {"A", "B", "C", "D"}
Dim arr5(1 To 2) As Integer = {1, 2}
 
Dim ub As Integer = min(UBound(arr1), min(UBound(arr4), UBound(arr5)))
For i As Integer = 1 To ub
Print arr1(i) & arr2(i) & arr3(i)
Next
 
Print
Sleep</syntaxhighlight>
 
{{out}}
<pre>
aA1
bB2
cC3
 
aA1
bB2
</pre>
 
==={{header|Gambas}}===
'''[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 a2 As String[] = ["A", "B", "C"]
Dim a3 As String[] = ["1", "2", "3"]
Dim siC As Short
 
For siC = 0 To a1.Max
Print a1[siC] & a2[siC] & a3[siC]
Next
 
End</syntaxhighlight>
 
Output:
<pre>
aA1
bB2
cC3
</pre>
 
==={{header|Liberty BASIC}}===
<syntaxhighlight lang="lb">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</syntaxhighlight>
 
==={{header|NS-HUBASIC}}===
<syntaxhighlight lang="ns-hubasic">10 DIM A$(3)
20 DIM B$(3)
30 DIM C$(3)
40 A$(1)="THIS"
50 A$(2)=" LOOPS"
60 A$(3)=" ARRAYS"
70 B$(1)=" NS-HUBASIC"
80 B$(2)=" OVER"
90 B$(3)=" AT"
100 C$(1)=" PROGRAM"
110 C$(2)=" MULTIPLE"
120 C$(3)=" ONCE."
130 FOR I=1 TO 3
140 PRINT A$(I)B$(I)C$(I)
150 NEXT</syntaxhighlight>
 
==={{header|PowerBASIC}}===
<syntaxhighlight 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</syntaxhighlight>
 
==={{header|PureBasic}}===
<syntaxhighlight lang="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</syntaxhighlight>
 
If they have different lengths there are two cases:<br>
a() is the shortest one: Only elements up to maximum index of a() are
printed <br>
a() is bigger than another one: if exceeding index to much, program
crashes, <br>
else it may work because there is some "free space" after end of
assigned array memory. <br>
For example if a has size 4, line dD4 will also be printed. size 20
leads to an crash <br>
This is because ReDim becomes slow if everytime there is a change to
array size new memory has to be allocated.
 
==={{header|Run BASIC}}===
<syntaxhighlight lang="runbasic">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</syntaxhighlight>
 
==={{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.
<syntaxhighlight lang="vbnet">
Module Program
Sub Main()
Dim a As Char() = {"a"c, "b"c, "c"c}
Dim b As Char() = {"A"c, "B"c, "C"c}
Dim c As Integer() = {1, 2, 3}
 
Dim minLength = {a.Length, b.Length, c.Length}.Min()
For i = 0 To minLength - 1
Console.WriteLine(a(i) & b(i) & c(i))
Next
 
Console.WriteLine()
 
For Each el As (a As Char, b As Char, c As Integer) In a.Zip(b, Function(l, r) (l, r)).Zip(c, Function(x, r) (x.l, x.r, r))
Console.WriteLine(el.a & el.b & el.c)
Next
End Sub
End Module</syntaxhighlight>
 
{{out}}
<pre>aA1
bB2
cC3
 
aA1
bB2
cC3</pre>
 
==={{header|XBasic}}===
{{works with|Windows XBasic}}
<syntaxhighlight lang="xbasic">' Loop over multiple arrays simultaneously
PROGRAM "loopoverarrays"
 
DECLARE FUNCTION Entry()
 
FUNCTION Entry()
DIM arr1$[2], arr2$[2], arr3%[2]
arr1$[0] = "a": arr1$[1] = "b": arr1$[2] = "c"
arr2$[0] = "A": arr2$[1] = "B": arr2$[2] = "C"
arr3%[0] = 1: arr3%[1] = 2: arr3%[2] = 3
FOR i% = 0 TO 2
PRINT arr1$[i%]; arr2$[i%]; FORMAT$("#", arr3%[i%])
NEXT i%
END FUNCTION
END PROGRAM
</syntaxhighlight>
 
==={{header|ZX Spectrum Basic}}===
<syntaxhighlight lang="zxbasic">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"</syntaxhighlight>
 
Simplification
 
<syntaxhighlight lang="zxbasic">10 READ size: DIM a$(size): DIM b$(size): DIM c$(size)
20 FOR i=1 TO size
30 READ a$(i),b$(i),c$(i)
40 PRINT a$(i);b$(i);c$(i)
50 NEXT i
60 DATA 3,"a","A","1","b","B","2","c","C","3"</syntaxhighlight>
 
=={{header|Beads}}==
Line 1,675 ⟶ 1,911:
 
If instead of reading the action had been to store a value into the array, then in the absence of bound checking, arbitrary damage will be done (to code or data) that will possibly result in something going wrong. And if you're lucky, it will happen swiftly.
 
=={{header|FreeBASIC}}==
<syntaxhighlight lang="freebasic">' FB 1.05.0 Win64
 
Function min(x As Integer, y As Integer) As Integer
Return IIf(x < y, x, y)
End Function
 
Dim arr1(1 To 3) As String = {"a", "b", "c"}
Dim arr2(1 To 3) As String = {"A", "B", "C"}
Dim arr3(1 To 3) As Integer = {1, 2, 3}
 
For i As Integer = 1 To 3
Print arr1(i) & arr2(i) & arr3(i)
Next
 
Print
 
' For arrays of different lengths we would need to iterate up to the mimimm length of all 3 in order
' to get a contribution from each one. For example:
 
Dim arr4(1 To 4) As String = {"A", "B", "C", "D"}
Dim arr5(1 To 2) As Integer = {1, 2}
 
Dim ub As Integer = min(UBound(arr1), min(UBound(arr4), UBound(arr5)))
For i As Integer = 1 To ub
Print arr1(i) & arr2(i) & arr3(i)
Next
 
Print
Sleep</syntaxhighlight>
 
{{out}}
<pre>
aA1
bB2
cC3
 
aA1
bB2
</pre>
 
=={{header|Frink}}==
Line 1,733 ⟶ 1,928:
{{out}}
 
<pre>
aA1
bB2
cC3
</pre>
 
=={{header|Gambas}}==
'''[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 a2 As String[] = ["A", "B", "C"]
Dim a3 As String[] = ["1", "2", "3"]
Dim siC As Short
 
For siC = 0 To a1.Max
Print a1[siC] & a2[siC] & a3[siC]
Next
 
End</syntaxhighlight>
 
Output:
<pre>
aA1
Line 2,521 ⟶ 2,695:
function with something like <code lisp>(: lists map
...)</code>.
 
=={{header|Liberty BASIC}}==
<syntaxhighlight lang="lb">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</syntaxhighlight>
 
=={{header|Lisaac}}==
Line 2,897 ⟶ 3,061:
for i in 0..2:
echo a[i], b[i], c[i]</syntaxhighlight>
 
=={{header|NS-HUBASIC}}==
<syntaxhighlight lang="ns-hubasic">10 DIM A$(3)
20 DIM B$(3)
30 DIM C$(3)
40 A$(1)="THIS"
50 A$(2)=" LOOPS"
60 A$(3)=" ARRAYS"
70 B$(1)=" NS-HUBASIC"
80 B$(2)=" OVER"
90 B$(3)=" AT"
100 C$(1)=" PROGRAM"
110 C$(2)=" MULTIPLE"
120 C$(3)=" ONCE."
130 FOR I=1 TO 3
140 PRINT A$(I)B$(I)C$(I)
150 NEXT</syntaxhighlight>
 
=={{header|Oberon-2}}==
Line 3,260 ⟶ 3,407:
[[/a /b /c] [/A /B /C] [1 2 3]] transpose
</syntaxhighlight>
 
=={{header|PowerBASIC}}==
<syntaxhighlight 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</syntaxhighlight>
 
=={{header|PowerShell}}==
Line 3,341 ⟶ 3,463:
false.
</pre>
 
=={{header|PureBasic}}==
<syntaxhighlight lang="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</syntaxhighlight>
 
If they have different lengths there are two cases:<br>
a() is the shortest one: Only elements up to maximum index of a() are
printed <br>
a() is bigger than another one: if exceeding index to much, program
crashes, <br>
else it may work because there is some "free space" after end of
assigned array memory. <br>
For example if a has size 4, line dD4 will also be printed. size 20
leads to an crash <br>
This is because ReDim becomes slow if everytime there is a change to
array size new memory has to be allocated.
 
=={{header|Python}}==
Line 3,659 ⟶ 3,752:
irb(main):002:0> ['a','b','c'].zip(['A','B'], [1,2,3,4])
=> [["a", "A", 1], ["b", "B", 2], ["c", nil, 3]]</syntaxhighlight>
 
=={{header|Run BASIC}}==
<syntaxhighlight lang="runbasic">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</syntaxhighlight>
 
=={{header|Rust}}==
Line 4,331 ⟶ 4,413:
stdout.printf("%c%c%i\n", a1[i], a2[i], a3[i]);
}</syntaxhighlight>
 
=={{header|VBA}}==
{{works with|VBA|VBA Excel 2013}}
<syntaxhighlight lang="vb">' Loop over multiple arrays simultaneously - VBA - 08/02/2021
 
Sub Main()
a = Array("a","b","c")
b = Array("A","B","C")
c = Array(1,2,3)
For i = LBound(a) To UBound(a)
buf = buf & vbCrLf & a(i) & b(i) & c(i)
Next
Debug.Print Mid(buf,3)
End Sub </syntaxhighlight>
{{out}}
<pre>
aA1
bB2
cC3
</pre>
 
=={{header|VBScript}}==
Line 4,369 ⟶ 4,431:
</pre>
 
=={{header|VBA}}==
{{works with|VBA|VBA Excel 2013}}
<syntaxhighlight lang="vb">' Loop over multiple arrays simultaneously - VBA - 08/02/2021
 
Sub Main()
=={{header|Visual Basic .NET}}==
a = Array("a","b","c")
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.
b = Array("A","B","C")
<syntaxhighlight lang="vbnet">
c = Array(1,2,3)
Module Program
For i = LBound(a) SubTo MainUBound(a)
buf = buf & vbCrLf Dim& a(i) As& Charb(i) =& {"a"c, "b"c, "c"c}(i)
Next
Dim b As Char() = {"A"c, "B"c, "C"c}
Debug.Print Mid(buf,3)
Dim c As Integer() = {1, 2, 3}
End Sub </syntaxhighlight>
 
Dim minLength = {a.Length, b.Length, c.Length}.Min()
For i = 0 To minLength - 1
Console.WriteLine(a(i) & b(i) & c(i))
Next
 
Console.WriteLine()
 
For Each el As (a As Char, b As Char, c As Integer) In a.Zip(b, Function(l, r) (l, r)).Zip(c, Function(x, r) (x.l, x.r, r))
Console.WriteLine(el.a & el.b & el.c)
Next
End Sub
End Module</syntaxhighlight>
 
{{out}}
<pre>aA1
aA1
bB2
cC3
</pre>
 
aA1
bB2
cC3</pre>
 
=={{header|Visual FoxPro}}==
Line 4,537 ⟶ 4,587:
xor eax, eax
ret
</syntaxhighlight>
 
=={{header|XBasic}}==
{{works with|Windows XBasic}}
<syntaxhighlight lang="xbasic">' Loop over multiple arrays simultaneously
PROGRAM "loopoverarrays"
 
DECLARE FUNCTION Entry()
 
FUNCTION Entry()
DIM arr1$[2], arr2$[2], arr3%[2]
arr1$[0] = "a": arr1$[1] = "b": arr1$[2] = "c"
arr2$[0] = "A": arr2$[1] = "B": arr2$[2] = "C"
arr3%[0] = 1: arr3%[1] = 2: arr3%[2] = 3
FOR i% = 0 TO 2
PRINT arr1$[i%]; arr2$[i%]; FORMAT$("#", arr3%[i%])
NEXT i%
END FUNCTION
END PROGRAM
</syntaxhighlight>
 
Line 4,648 ⟶ 4,679:
try std.io.getStdOut().writer().print("{c} {c} {d}\n", .{ a1[i], a2[i], a3[i] });
}</syntaxhighlight>
 
=={{header|ZX Spectrum Basic}}==
 
<syntaxhighlight lang="zxbasic">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"</syntaxhighlight>
 
Simplification
 
<syntaxhighlight lang="zxbasic">10 READ size: DIM a$(size): DIM b$(size): DIM c$(size)
20 FOR i=1 TO size
30 READ a$(i),b$(i),c$(i)
40 PRINT a$(i);b$(i);c$(i)
50 NEXT i
60 DATA 3,"a","A","1","b","B","2","c","C","3"</syntaxhighlight>
 
{{omit from|PL/0}}
{{omit from|Tiny BASIC}}
2,130

edits