Announcement

Collapse
No announcement yet.

ARRAY INSERT with multidimenensional arrays

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Ian Bayly
    replied
    That's it! Thanks Gösta, I should have thought of the REDIM!!
    Enjoy Christmas!
    Ian B

    Leave a comment:


  • Gösta H. Lovgren-2
    replied
    Originally posted by Ian Bayly View Post
    I would further have expected the UBOUND to increase by 1.
    Any other suggestions greatly appreciated.

    Ian B
    Array Insert works by moving everything up one element (discarding the last element and inserting a value (string or numeric as applicable) into the into the array. It doesn't increase the array size. To make the array larger, Redim (Preserve) is needed to save the last element. ie
    Code:
     
    Redim Preserve a(ubound (a() + 1) 
    Array Insert a() ....
    (note I don't think you can increase an inside dimension, only the outermost dimension in a multi-dimmed array)

    ============================================
    "Whatever is begun in anger ends in shame."
    Benjamin Franklin (1706-1790)
    ============================================
    Last edited by Gösta H. Lovgren-2; 18 Dec 2008, 09:19 PM.

    Leave a comment:


  • Ian Bayly
    replied
    Thanks for your help Gösta, I see things somewhat more clearly.

    I have added these lines to the code:
    Array Insert sTest(0,1) For 2, "Code88"
    Array Insert sTest(1,1) For 2, "BLUE"
    Array Insert sTest(2,1) For 2, "Clover"
    Array Insert sTest(3,1) For 2, "33"
    and would have expected the result to look like this:
    sTest(0,0)="Code12"
    sTest(1,0)="BLACK"
    sTest(2,0)="Butterflies"
    sTest(3,0)="3"
    sTest(0,1)="Code88"
    sTest(1,1)="BLUE"
    sTest(2,1)="Clover"
    sTest(3,1)="33"
    sTest(0,2)="Code15"
    sTest(1,2)="RED"
    sTest(2,2)="Agapanthus"
    sTest(3,2)="12"
    sTest(0,3)="Code77"
    sTest(1,3)="GREY"
    sTest(2,3)="Fennel"
    sTest(3,3)="2
    I would further have expected the UBOUND to increase by 1.
    I think I need to experiment with the FOR values.
    Any other suggestions greatly appreciated.

    Ian B

    Leave a comment:


  • Gösta H. Lovgren-2
    replied
    Try this Ian,
    Code:
    Function PBMain () As Long
    Dim sTest(3,2) As String
    Local s As String, ctr As Long
    sTest(0,0)="Code12"
    sTest(1,0)="BLACK"
    sTest(2,0)="Butterflies"
    sTest(3,0)="3"
    sTest(0,1)="Code15"
    sTest(1,1)="RED"
    sTest(2,1)="Agapanthus"
    sTest(3,1)="12"
    sTest(0,2)="Code77"
    sTest(1,2)="GREY"
    sTest(2,2)="Fennel"
    sTest(3,2)="2"                           
     ErrClear
    'MsgBox sTest(2,2) & " " & Str$(UBound(sTest,2)),,"Before Insert"
      GoSub Set_s
      ? s$,, "Before Insert"
    Array Insert sTest(0,1) For 2, "test Insert"
    'MsgBox sTest(0,2) & " " & Str$(UBound(sTest,2)),,
      GoSub Set_s
      ? s$,, "After Insert"
      If Err Then
        ?Error$(Err) & Str$(Err),, "error"
      End If
     Exit Function
     
    Set_s:
     
     Reset s$
     For ctr = 0 To 3
       s$ = s$ &  sTest(ctr,1) & $CrLf 
     Next ctr
     Return
    End Function

    Leave a comment:


  • Ian Bayly
    started a topic ARRAY INSERT with multidimenensional arrays

    ARRAY INSERT with multidimenensional arrays

    I've read and reread the Help, but still fail to fully understand the phrase "stored in linear column-major order".
    I have a multi-dimensional array, where the need to REDIM PRESERVE dictates the array structure.
    The code below does not insert a blank array element as I would have expected.
    Can any one point to my error. Sorry about Header spelling, but I can't get at it to fix!
    TIA

    Ian B

    Code:
    FUNCTION PBMAIN () AS LONG
    DIM sTest(3,2)  AS STRING
    sTest(0,0)="Code12"
    sTest(1,0)="BLACK"
    sTest(2,0)="Butterflies"
    sTest(3,0)="3"
    sTest(0,1)="Code15"
    sTest(1,1)="RED"
    sTest(2,1)="Agapanthus"
    sTest(3,1)="12"
    sTest(0,2)="Code77"
    sTest(1,2)="GREY"
    sTest(2,2)="Fennel"
    sTest(3,2)="2"
    MSGBOX sTest(2,2) & " " & STR$(UBOUND(sTest,2))
    ARRAY INSERT sTest(0,1) FOR 2
    MSGBOX sTest(0,2) & " " & STR$(UBOUND(sTest,2))
    END FUNCTION
Working...
X