about array

Post

Posted
Rating:
#1 (In Topic #2011)
Avatar
Trainee
monkeyking is in the usergroup ‘unknown’

Dim areas As New Integer[][3], how to determine the dimension, to assign values to the elements

give me a demo ofa jagged array
Attachmentgambas_array.txt
 
Online now: No Back to the top

Post

Posted
Rating:
#2
Avatar
Enthusiast
GrayGhost is in the usergroup ‘Enthusiast’
I have not used multi dimension    but I think this is how


 Dim areas As New Integer[3,3,3]


Read this thread , more complete explanation :

Dynamic Multidimensional Arrays - Gambas ONE

Last edit: by GrayGhost

Online now: No Back to the top

Post

Posted
Rating:
#3
Avatar
Enthusiast
Gianluigi is in the usergroup ‘Enthusiast’
Gianluigi is in the usergroup ‘GambOS Contributor’
Hi monkeyking

If you search on the Farm, there's an old demonstration of mine (I was new to Gambas).
DynamicMatrixExample is its name.

 :goodbye:
Online now: No Back to the top

Post

Posted
Rating:
#4
Avatar
Administrator
gbWilly is in the usergroup ‘unknown’
gbWilly leads the usergroup ‘GambOS Contributor’
gbWilly is in the usergroup ‘Blogger’

monkeyking said

give me a demo ofa jagged array
You wanted jagged, your demo code is NOT jagged as all arrays are equal lenght.
Array in below example holds in the first record an array of 3 items, in the second record an array of 6 items.
Just look at attached source archive on how to go about when creating jagged arrays.

Example of a jagged array dynamicly determined in code on the fly:
Attachment

Array-0.0.1.tar.gz



You can take it anywhere you want, once you understand how Gambas does things.:thumbs:

Enjoy…

Jagged arrays explained for those who don't know what it's about

 

gbWilly
- Gambas Dutch translator
- Gambas wiki content contributor
- Gambas debian/ubuntu package recipe contributor
- GambOS, a distro for learning Gambas and more…
- Gambas3 Debian/Ubuntu repositories


… there is always a Catch if things go wrong!
Online now: No Back to the top

Post

Posted
Rating:
Item has a rating of 5 (Liked by gbWillyLiked by Yogi)
#5
Avatar
Enthusiast
Gianluigi is in the usergroup ‘Enthusiast’
Gianluigi is in the usergroup ‘GambOS Contributor’
Hi, monkeyking

Continuing with the example I posted years ago, perhaps this code, along with gbWilly's important explanations, can help you understand matrices:

Code

Public Sub Main()

  Print "Hello world"
  Print "-----------"
  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

End

 :goodbye:
Online now: No Back to the top

Post

Posted
Rating:
#6
Avatar
Regular
ercoupeflyer is in the usergroup ‘Regular’
ercoupeflyer is in the usergroup ‘Blogger’
Here is the explanation of what is happening in your original program as well as the corrected program regarding what you are attempting to do
I hope it helps
' Gambas module file

Public Sub Main()

    Dim areas As New Integer[][3]  ' doing this causes gambas to create the dynamic array areas, then create 3 uninitialized entries Null

    Print areas.MAX, areas.dim  ' this returns the linear number of elements total for each entry elements contained in the row
    ' so it now looks like this, it is not doing what you are expecting
    ' element #   , Value, now defined in areas1
    '  [0]      NULL
    '  [1]      NULL
    '  [2]      NULL
    ' So when you add the elements  areas.Add([1, 1, 1]) ,areas.Add([2, 2, 2])
    ' it looks like this
    ' [0] NULL
    ' [1] NULL
    ' [2] NULL
    ' [3] [1,1,1]
    ' [4] [2,2,2]
    ' then after you do this areas[0] = [3, 3, 3], areas[1] = [4, 4, 4]
    ' it looks like this
    ' [0] [3,3,3]
    ' [1] [4,4,4]
    ' [2] NULL
    ' [3] [1,1,1]
    ' [4] [2,2,2]
    ' therefore when you try to iterate through them you get a null pointer error
    ' the correct way to do this is as follows
    Dim areas1 As New Integer[][]
    areas1.Add([1, 1, 1]) ' this adds an entry to the array 
    areas1.Add([2, 2, 2]) ' this adds another one
    ' You now have the
    Print areas1.MAX, areas1.dim  ' this returns the linear number of elements total
    ' then you are changing the values as follows
    areas1[0] = [3, 3, 3] ' these both change the entries you have already added
    areas1[1] = [4, 4, 4]

    Print areas.MAX, areas.dim  ' this returns the linear number of elements total
    ' this method is ok
    For i As Integer = 0 To areas1.max
        For j As Integer = 0 To areas1[i].max
            Print areas1[i][j];;
        Next
        Print
    Next
    Print
    'this may be better
    For Each area As Integer[] In areas1
        For Each entry As Integer In area
            Print entry;;
        Next
        Print
    Next

End
The output now looks like this
2       1
1       1
2       1

3 3 3 
4 4 4 

3 3 3 
4 4 4 
 
I hope this helps you to understand what is happening, and how to correct it.
If you want to actually control the entry type to 3 elements it may be better to create a class of area_entry and then an array of  'areas as area_entry[]'
You can cut and paste between the lines into your ide and give it a try

 

Last edit: by ercoupeflyer

Online now: No Back to the top

Post

Posted
Rating:
#7
Avatar
Enthusiast
Gianluigi is in the usergroup ‘Enthusiast’
Gianluigi is in the usergroup ‘GambOS Contributor’

ercouperflyer said

 'this may be better
    For Each area As Integer[] In areas1
        For Each entry As Integer In area
            Print entry;;
        Next
        Print
    Next

Although rare and unlikely, in Gambas3, using FOR EACH can alter the array order.
So your assertion that using FOR EACH is preferable to looping with counters is incorrect.

 :goodbye:
Online now: No Back to the top

Post

Posted
Rating:
#8
Avatar
Regular
ercoupeflyer is in the usergroup ‘Regular’
ercoupeflyer is in the usergroup ‘Blogger’
Just a small fact, internally Gambas treats all arrays as dynamic single dimension arrays, it uses the supplied dims as a math tool
for fetching the correct value, but you can just as well add more data. In this example I add more data after the dimensions set
this is also quite valid

Dim exp As New Integer[5, 3, 4] ' static array definition
    exp.Add(100)
    exp.Add(200)
    For Each i As Integer In exp
        Print i;;
    Next
    Print
    Print exp.MAX, exp.dim
outputs:
 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 100 200
61      3
But you are unable to access those new elements using the exp[n,n,n] method.
Online now: No Back to the top

Post

Posted
Rating:
#9
Avatar
Regular
ercoupeflyer is in the usergroup ‘Regular’
ercoupeflyer is in the usergroup ‘Blogger’

Gianluigi said

ercouperflyer said

 'this may be better
    For Each area As Integer[] In areas1
        For Each entry As Integer In area
            Print entry;;
        Next
        Print
    Next

Although rare and unlikely, in Gambas3, using FOR EACH can alter the array order.
So your assertion that using FOR EACH is preferable to looping with counters is incorrect.

 :goodbye:

How does that happen, certainly that will happen with variant arrays and object collections, but int arrays are stored in memory as a linear array of values.
I would like to understand how they become out of order. Can you provide an example.

I have been implementing production dynamic numeric arrays in this way for years, You have given me a bit of a scare here, I have done a lot of work. I have often sorted
arrays and used this method for listing the results, never had an issue. I really need a solid example of how this may occur.
Online now: No Back to the top

Post

Posted
Rating:
#10
Avatar
Enthusiast
Gianluigi is in the usergroup ‘Enthusiast’
Gianluigi is in the usergroup ‘GambOS Contributor’
Hi ercoupeflyer

I don't know what to say; I'm an amateur who, on top of everything, hasn't studied algebra.
You won't get anything from me, except the little I know about Gambas.
I tried to find Benoit's email where he said this, but after two hours of fruitless searching, I gave up. So I asked about ML and got this reply:
I am going to interpret your "correct sequence" as "expected sequence". It is true. However rare to encounter.

There is chance that after manipulating an array (in many ways) that the order in which For Each iterates is not the same as the last "order" as determined by the operations on the array.

However, it is more a "beware of" rather than a rule.

I believe that he was just making people aware of this, rather than trying to scare them.

b

 :goodbye:
Online now: No Back to the top

Post

Posted
Rating:
#11
Avatar
Regular
ercoupeflyer is in the usergroup ‘Regular’
ercoupeflyer is in the usergroup ‘Blogger’

Gianluigi said

Hi ercoupeflyer

I don't know what to say; I'm an amateur who, on top of everything, hasn't studied algebra.
You won't get anything from me, except the little I know about Gambas.
I tried to find Benoit's email where he said this, but after two hours of fruitless searching, I gave up. So I asked about ML and got this reply:
I am going to interpret your "correct sequence" as "expected sequence". It is true. However rare to encounter.

There is chance that after manipulating an array (in many ways) that the order in which For Each iterates is not the same as the last "order" as determined by the operations on the array.

However, it is more a "beware of" rather than a rule.

I believe that he was just making people aware of this, rather than trying to scare them.

b

 :goodbye:


who is the  B that made that comment
Online now: No Back to the top

Post

Posted
Rating:
#12
Avatar
Enthusiast
Gianluigi is in the usergroup ‘Enthusiast’
Gianluigi is in the usergroup ‘GambOS Contributor’

ercoupeflayer said

 who is the  B that made that comment

I think he's someone who, like me, remembered Benoit's warning.

Anyway, here's what (little) has been written about ML:
Looping through an array with FOR EACH

 :goodbye:
Online now: No Back to the top

Post

Posted
Rating:
#13
Avatar
Regular
ercoupeflyer is in the usergroup ‘Regular’
ercoupeflyer is in the usergroup ‘Blogger’
So Ben says that the for each is completely reliable, to return the array in indexed order.
Quote:

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.


The issue he has mentioned is if you change the array content while you are enumerating it, then of course you will break the enumeration.

Of course the same would happen if you changed the array content when you were doing the

for i = 0 to xx
  for j = 0 to xx
   print a[i,j]
  next
next

So 'for each' is a completely reliable way of enumerating arrays

Online now: No Back to the top

Post

Posted
Rating:
Item has a rating of 5 (Liked by gbWilly)
#14
Avatar
Enthusiast
GrayGhost is in the usergroup ‘Enthusiast’
I am glad you presued that question …. I would hate to not sleep tonight worring about it :)
Online now: No Back to the top

Post

Posted
Rating:
Item has a rating of 5 (Liked by gbWilly)
#15
Avatar
Enthusiast
Gianluigi is in the usergroup ‘Enthusiast’
Gianluigi is in the usergroup ‘GambOS Contributor’
Hi ercoupeflyer

Yes, you're right, as usual, I misunderstood what Benoit meant.
But it's also worth noting what he later clarified about the For Each loop:

Benoit said

Better for what?

"For Each" on an array is usually slower than using a "For…To" loop with an index variable, except if you don't need the index. Check by yourself anyway according to what you need exactly.

And the JIT compiler do not optimize the "For Each" loop.

Otherwise it's not important how you browse the array.

Regards,


Benoît Minisini.

Anyway, I think we've all learned something from this discussion.
The bad thing is that tomorrow (let's say the day after tomorrow) I'll have already forgotten about it.
It may not seem like it, but age brings its advantages.  O_o

 :goodbye:
Online now: No Back to the top

Post

Posted
Rating:
#16
Avatar
Regular
ercoupeflyer is in the usergroup ‘Regular’
ercoupeflyer is in the usergroup ‘Blogger’
As I explain on the mailing list, I was only talking about the example i presented, and suggested the 'for each' as a simpler way to
print a dynamic array with fixed sub array elements. In my opinion.

And yes best to forget this. But thank you for helping to making this an in depth and complete dicussion.

It is true that age has it's wisdoms and graces, it is often difficult to embrace them both.
Online now: No Back to the top

Post

Posted
Rating:
#17
Avatar
Enthusiast
Gianluigi is in the usergroup ‘Enthusiast’
Gianluigi is in the usergroup ‘GambOS Contributor’
…and if anyone is interested in my answer, here's the link to the discussion:
about array - Gambas ONE
Online now: No Back to the top

Post

Posted
Rating:
Item has a rating of 5 (Liked by gbWillyLiked by Gianluigi)
#18
Avatar
Regular
ercoupeflyer is in the usergroup ‘Regular’
ercoupeflyer is in the usergroup ‘Blogger’
Here is a test of the different method of iterating a complex array, looks like a hybrid approach is fastest.
My answer as it is,
Ranking of each type by time
1       For each hybrid Time= 0.760726280, Total=9000003        0.760726279811934
2       Index only          Time= 0.841511623, total=9000003        0.841511623002589
3       for each only     Time= 1.253503899, Total=9000003        1.25350389885716
The program used for testing:
' Gambas module file

Public Sub Main()

    Dim Ranking As New Collection(gb.ignorecase)
    Dim areas1 As New Integer[][]
    ' Lets add a bunch Of entries To the arrays
    For p As Integer = 0 To 3000000
        areas1.Add([1, 1, 1])
    Next

    ' just indexing
    Dim total As Long = 0
    Dim starttime As Float = Timer
    For i As Integer = 0 To areas1.max
        For j As Integer = 0 To areas1.max
            total += areas1[j]
        Next
    Next
    Dim indextime As Float = Timer - starttime
    Ranking.Add(indextime, Subst("Index only      Time=&1, total=&2 ", Format(indextime, "##.########0"), total))

    ' just for each this is slower
    total = 0
    starttime = Timer
    For Each areaA As Integer[] In areas1
        For Each entry As Integer In areaA
            total += entry
        Next
    Next
    Dim puretime As Float = Timer - starttime
    Ranking.Add(puretime, Subst("for each only   Time=&1, Total=&2", Format(puretime, "##.########0"), total))

    'hybrid this may be better
    total = 0
    StartTime = Timer
    For Each area As Integer[] In areas1
        For i = 0 To area.max
            total += area
        Next
    Next
    Dim foreachtime As Float = Timer - starttime
    Ranking.Add(foreachtime, Subst("For each hybrid Time=&1, Total=&2", Format(foreachtime, "##.########0"), total))

    'Display the ranking
    Dim rankindex As Integer = 1
    Dim sortitout As String[] = Ranking.keys.Sort()
    Print "Ranking of each type by time"
    For Each s As String In sortitout
        Print rankindex, s, Ranking
        inc rankindex
   next
end




 
Online now: No Back to the top
1 guest and 0 members have just viewed this.