[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Looping through an array with FOR EACH
[Thread Prev] | [Thread Next]
- Subject: Re: Looping through an array with FOR EACH
- From: Brian G <brian@xxxxxxxxxxxxxxxx>
- Date: Thu, 15 Jan 2026 16:41:07 -0800
- To: user@xxxxxxxxxxxxxxxxxxxxxx
On 1/15/26 15:21, Gianluigi wrote:
It was not Ben that suggested that It was me, and it was because the fellow wanted to use fixed array size entry within a dynamic array.Il 15/01/26 23:26, Benoît Minisini ha scritto:Sorry, I never thought I would have to explain that, because arrays work the same way in every language - except javascript, but who said is was a serious language?An array is a list of values of the same datatypes that are stored consecutively in memory. Why? Because it's the easiest and fastest way to store and access its contents.It's a basic data structure that is even often handled by specific CPU instructions, especially on x86.And so values are indexed by their position, and so it's logical to enumerate them in the index order. Which other order could you use?And of course, if you modify the array while you enumerate it, you must use your brain to understand which wrong things could happen.Regards, -- Benoît Minisini.Dear Benoit, I apologize for creating all this confusion.It all started with a forum user writing that it would have been better to loop through the array with For Each.Then I remembered your warning (which, as usual, I misunderstood).But at this point, you need to explain to me why the FOR EACH loop would be better than the one I suggested:[code] Public Sub Main() Dim aAreas As New String[][] ' The dynamic matrix Dim r, c, e, i As Integer' Array of support to the random filling of the matrix with pseudo names Dim aNomi As String[] = ["Ful", "Gal", "Doc", "Ech", "All", "Bil", "Val", "Mal", "Cec", "Lol", "Nib"]r = 5 ' six rows ' Initializes the random number generator Randomize ' Loop to populate the dynamic array For e = 0 To r ' Resize the array aAreas.Resize(e + 1) ' Instantiates a new internal array aAreas[e] = New String[] ' random number of columns c = Rand(0, 8) ' Loop to populate the array For i = 0 To c ' Resize the array aAreas[e].Resize(i + 1)' Generate random names, but with number of row and column for the control aAreas[e][i] = aNomi[Rand(0, 10)] & "(C" & CStr(i + 1) & " R" & CStr(e + 1) & ")"Next Next ' Read the jagged matrix For r = 0 To aAreas.Max For c = 0 To aAreas[r].Max Print aAreas[r][c];; Next Print Next ' This may be better <----------------------??? Print "==============================================================" For Each area As String[] In aAreas For Each entry As String In area Print entry;; Next Print Next End [/code] With Best Regards Gianluigi
I suggested it because it was easier to understand. I was not commenting on your code, only on the code I presented of which you only quote the last paragraph example
For Each area As String[] In aAreas For Each entry As String In area Print entry;; Next Print Next -- ~~~~ Brian
Attachment:
OpenPGP_0x78BFB26402F48419.asc
Description: OpenPGP public key
Attachment:
OpenPGP_signature.asc
Description: OpenPGP digital signature
| Re: Looping through an array with FOR EACH | Jussi Lahtinen <jussi.lahtinen@xxxxxxxxx> |
| Re: Looping through an array with FOR EACH | Gianluigi <gradobag@xxxxxxxxxxx> |
| Looping through an array with FOR EACH | Gianluigi <gradobag@xxxxxxxxxxx> |
| Re: Looping through an array with FOR EACH | BB <adamnt42@xxxxxxxxx> |
| Re: Looping through an array with FOR EACH | Gianluigi <gradobag@xxxxxxxxxxx> |
| Re: Looping through an array with FOR EACH | Brian G <brian@xxxxxxxxxxxxxxxx> |
| Re: Looping through an array with FOR EACH | Gianluigi <gradobag@xxxxxxxxxxx> |
| Re: Looping through an array with FOR EACH | Benoît Minisini <benoit.minisini@xxxxxxxxxxxxxxxx> |
| Re: Looping through an array with FOR EACH | Gianluigi <gradobag@xxxxxxxxxxx> |