[Gambas-user] multidimensional array of integers

gaupe hennie at ...1826...
Tue Jan 15 12:00:37 CET 2008


Ron Thanks for pointing me in the right direction.
My mistake was apperently that i did not realise that it is not possible to
do this with a single statement.
if have some ancient experience with clipper5 an old database like language
and (i looked it up ;-)
it was as .... simple.. as

'asort( anArray,,,{|x,y| x[2] >= y[2] .AND. IIF( x[2] = y[2], x[1] <
y[1],.T.)})

you would think that after all those years these simple things would be
functions classes or whatever
these are called, easy to use as these are the basics. same with dynamical
multidimensional arrays.

I am of the opinion that one should not have to write these code for such
basic needs anymore.



btw the a.max does not seem to work also not a.max[0] or a[0].max
so i used a.bounds[0]  for that

here is the code that works for those who struggle with the same....



  DIM i AS Integer
  DIM a AS NEW Integer[5, 2]

a[0, 0] = 1
a[0, 1] = 5

a[1, 0] = 3
a[1, 1] = 4

a[2, 0] = 2
a[2, 1] = 2

a[3, 0] = 4
a[3, 1] = 3

a[4, 0] = 5
a[4, 1] = 1




PRINT "Unsorted List of 2d Array."
FOR i = 0 TO a.Bounds[0] - 1
  PRINT a[i, 0], a[i, 1]
NEXT

DualSorter(a, 1)

PRINT "Sorted List of 2d Array by Number."
FOR i = 0 TO a.Bounds[0] - 1
  PRINT a[i, 0], a[i, 1]
NEXT

DualSorter(a, 0)

PRINT "Sorted List of 2d Array by Name."
FOR i = 0 TO a.Bounds[0] - 1
  PRINT a[i, 0], a[i, 1] 
NEXT


END

PUBLIC FUNCTION DualSorter(a AS Integer[], DimensionToSort AS Integer) AS
Integer[]
   DIM column AS Integer = 0
   DIM row, j, StartingKeyValue, StartingOtherValue, NewStartingKey,
NewStartingOther, swap_pos, OtherDimension AS Integer
 
   ' Ensure that the user has picked a valid DimensionToSort
  IF DimensionToSort = 1 THEN
    OtherDimension = 0
  ELSE IF DimensionToSort = 0 THEN
    OtherDimension = 1
  ELSE
      'Shoot, invalid value of DimensionToSort
      PRINT "Invalid dimension for DimensionToSort: must be value of 1 or
0."
  END IF
    
    FOR row = 0 TO a.Bounds[column] - 1
    'Start outer loop.
    
        'Take a snapshot of the first element
        'in the array because if there is a 
        'smaller value elsewhere in the array 
        'we'll need to do a swap.
        StartingKeyValue = a[row, DimensionToSort]
        StartingOtherValue = a[row, OtherDimension]
        
        ' Default the Starting values to the First Record
        NewStartingKey = a[row, DimensionToSort]
        NewStartingOther = a[row, OtherDimension]
        
        swap_pos = row
    
        FOR j = row + 1 TO a.bounds[column] - 1
        'Start inner loop.
            IF a[j, DimensionToSort] < NewStartingKey THEN
            'This is now the lowest number - 
            'remember it's position.
                swap_pos = j
                NewStartingKey = a[j, DimensionToSort]
                NewStartingOther = a[j, OtherDimension]
            END IF
        NEXT
    
        IF swap_pos <> row THEN
        'If we get here then we are about to do a swap
        'within the array.
            a[swap_pos, DimensionToSort] = StartingKeyValue
            a[swap_pos, OtherDimension] = StartingOtherValue
            
            a[row, DimensionToSort] = NewStartingKey
            a[row, OtherDimension] = NewStartingOther
            
        END IF  
    NEXT
    RETURN a
END 







 

Ron Onstenk wrote:
> 
> On Tuesday 15 January 2008 02:12, gaupe wrote:
>> 
>> thanks for  answering.
>> It does however not bring me much further as these are examples of fixed
>> arrays
>> and there is no sorting done in your example
>> 
>> most important for me is the sorting , not for a one dimensional but a 2
>> dimensional as shown in the starting
>> post
>> then i would like to have dynamicly add records to it.
>> perhaps you know how to do that too?
>> this is bothering me allready for weeks. ;-)  i feel real stupid not
>> beeing
>> able to figure it out :-)
>> 
>> 
> 
> Sorting a One-Dimensional Array in gambas
> http://gambasdoc.org/help/comp/gb/string[]/sort
> 
> ----
> Sorting a Two-Dimensional Array 
> Simple give code without explain why and how make no sense.
> 
> See following pages. There will be shown howto do it.
> It is written for a alien basic ( :) ) but it show the
> technics to use. The method is usable with gambas.
> The article link is the explain for the method.
> The demo link is a page with the code only, almost
> ready to cut/paste for gambas.
> 
> 
> 
> Article:
> Sorting a Two-Dimensional Array using a Bubble Sort 
> http://www.4guysfromrolla.com/webtech/011601-1.shtml
> 
> demo:
> Two-Dimensional Array Sort Using Bubble Sort
> http://www.4guysfromrolla.com/demos/2dsort.asp
> 
> Article:
> Sorting a Two-Dimensional Array using a Bubble Sort, Part 2
> http://www.4guysfromrolla.com/webtech/011601-1.2.shtml
> 
> demo:
> Array of Objects Sorting Using Bubble Sort
> http://www.4guysfromrolla.com/demos/objsort.asp
> 
> Ron
> 
> ps
> 'Response.Write' can replaced with 'Print'
> LBound() and UBound() are the first and last index of in the array
> use for LBound 0 and for UBound YourArray.Max 
> (http://gambasdoc.org/help/comp/gb/array/max)
> 
> -------------------------------------------------------------------------
> Check out the new SourceForge.net Marketplace.
> It's the best place to buy or sell services for
> just about anything Open Source.
> http://ad.doubleclick.net/clk;164216239;13503038;w?http://sf.net/marketplace
> _______________________________________________
> Gambas-user mailing list
> Gambas-user at lists.sourceforge.net
> https://lists.sourceforge.net/lists/listinfo/gambas-user
> 
> 

-- 
View this message in context: http://www.nabble.com/multidimensional-array-of-integers-tp14803157p14837268.html
Sent from the gambas-user mailing list archive at Nabble.com.





More information about the User mailing list