[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Looping through an array with FOR EACH


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


Follow-Ups:
Re: Looping through an array with FOR EACHBenoît Minisini <benoit.minisini@xxxxxxxxxxxxxxxx>
Re: Looping through an array with FOR EACHBrian G <brian@xxxxxxxxxxxxxxxx>
References:
Looping through an array with FOR EACHGianluigi <gradobag@xxxxxxxxxxx>
Re: Looping through an array with FOR EACHBB <adamnt42@xxxxxxxxx>
Re: Looping through an array with FOR EACHGianluigi <gradobag@xxxxxxxxxxx>
Re: Looping through an array with FOR EACHBrian G <brian@xxxxxxxxxxxxxxxx>
Re: Looping through an array with FOR EACHGianluigi <gradobag@xxxxxxxxxxx>
Re: Looping through an array with FOR EACHBenoît Minisini <benoit.minisini@xxxxxxxxxxxxxxxx>