Announcement

Collapse

Forum Guidelines

This forum is for finished source code that is working properly. If you have questions about this or any other source code, please post it in one of the Discussion Forums, not here.
See more
See less

Insert or delete columns and rows in two-dimensional arrays

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

  • Insert or delete columns and rows in two-dimensional arrays

    ' Insert or delete columns and rows in two-dimensional arrays.
    '
    ' These simple routines are based on the PowerBasic ARRAY DELETE and ARRAY
    ' INSERT functions. Since these functions shift the elements only one place,
    ' the functions need to be called repeatedly to insert or delete an entire
    ' row or column. Since the functions are extremely fast, the delay is not
    ' conspicuous even for quite large arrays. For illustrative purposes "---"
    ' stands for blank elements (zero string). In real applications , " ---"
    ' should be removed from the routines. Comments are welcome.
    '
    ' Best Regards,
    '
    ' Erik
    Code:
    #COMPILE EXE
    #REGISTER NONE
    #DIM ALL
    '
    GLOBAL t AS STRING
    '
    FUNCTION PBMAIN
        LOCAL R&,C&
        R = 4 : C = 6
        DIM a(C,R) AS STRING
        '
        t = ""
        CALL  MakeArray(a(),C,R)
        t = t + "Delete column 2 (zero based)"+$CRLF
        CALL DeleteColumn(a(),2,C,R)
        CALL ShowResults(a(),C,R)
        '
        t = ""
        CALL  MakeArray(a(),C,R)
        t = t + "Delete row 1 (zero based)"+$CRLF
        CALL DeleteRow(a(),1,C,R)
        CALL ShowResults(a(),C,R)
        '
        t = ""
        CALL  MakeArray(a(),C,R)
        t = t + "Insert column 3 (zero based)"+$CRLF
        CALL InsertColumn(a(),3,C,R)
        CALL ShowResults(a(),C,R)
        '
        t = ""
        CALL  MakeArray(a(),C,R)
        t = t + "Insert row 2 (zero based)"+$CRLF
        CALL InsertRow(a(),2,C,R)
        CALL ShowResults(a(),C,R)
        '
    END FUNCTION
    '
    SUB MakeArray(BYREF a() AS STRING,BYVAL C AS LONG, BYVAL R AS LONG)
        LOCAL i&,j&
        RANDOMIZE 1.3 ' Ensures same sequence of numbers at each call.
        FOR j = 0 TO R ' Rows
            FOR i = 0 TO C ' Columns
                ' The following definition of row and column index allows
                ' redim preserve with addition of extra rows of data,
                ' i.e. column index is first, row index is second.
                a(i,j) = RIGHT$("    "+STR$(RND(0,9)),4)
                t=t+a(i,j)
            NEXT
            t=t+$CRLF
        NEXT
        t=t+$CRLF
    END SUB
    
    SUB ShowResults(BYREF a() AS STRING,BYVAL C AS LONG, BYVAL R AS LONG)
        LOCAL i&,j&
        FOR j = 0 TO R
            FOR i = 0 TO C
                t=t+a(i,j)
            NEXT
            t=t+$CRLF
        NEXT
        MSGBOX t
    END SUB
    '
    ' In the following routines "  ---" is being inserted for BLANK for
    ' illustrative purposes . In real applications , "  ---" should be
    ' removed from the routines.
    SUB DeleteColumn(BYREF a() AS STRING, BYVAL l AS LONG,BYVAL C AS LONG, BYVAL R AS LONG)
        LOCAL i AS LONG
        FOR i=0 TO R
            ARRAY DELETE a(l,i) FOR (C-l+1), "  ---"
        NEXT
    END SUB
    '
    SUB InsertColumn(BYREF a() AS STRING, BYVAL l AS LONG,BYVAL C AS LONG, BYVAL R AS LONG)
        LOCAL i AS LONG
        FOR i=0 TO R
            ARRAY INSERT a(l,i) FOR (C-l+1), "  ---"
        NEXT
    END SUB
    '
    SUB DeleteRow(BYREF a() AS STRING, BYVAL l AS LONG,BYVAL C AS LONG, BYVAL R AS LONG)
        LOCAL i AS LONG
        FOR i=0 TO C
            ARRAY DELETE a(0,l) FOR (R-l+1)*(C+1)-i, "  ---"
        NEXT
    END SUB
    '
    SUB InsertRow(BYREF a() AS STRING, BYVAL l AS LONG,BYVAL C AS LONG, BYVAL R AS LONG)
        LOCAL i AS LONG
        FOR i=0 TO C
            ARRAY INSERT a(i,l) FOR (R-l+1)*(C+1)-i, "  ---"
        NEXT
    END SUB
    [This message has been edited by Erik Christensen (edited October 13, 2002).]

  • #2
    ' Insert or delete columns and rows in two-dimensional arrays
    ' using row and column indices.
    '
    ' For very large arrays the method presented above using direct manipulation
    ' of the data array is too slow.
    '
    ' For large arrays a better approach is to use indices to determine which
    ' columns and rows should actually be displayed. Furthermore, the array
    ' should be dimensioned to include a certain extra amount of empty rows
    ' and columns. By manipulating indices the display can be defined according
    ' to the need without actually changing the place of any data item in the
    ' array. For this reason the method is very fast and efficient. For
    ' illustrative purposes "---" stands for blank elements (zero string).

    ' Good luck.
    '
    ' Erik
    Code:
    #COMPILE EXE
    #REGISTER NONE
    #DIM ALL
    '
    GLOBAL t AS STRING
    GLOBAL CI() AS LONG  ' Column index array
    GLOBAL RI() AS LONG  ' Row index array
    GLOBAL CE AS LONG    ' Number of extra columns for manipulating data
    GLOBAL RE AS LONG    ' Number of extra rows for manipulating data
    '
    FUNCTION PBMAIN
        LOCAL C&         ' Number of columns in the 2D-array
        LOCAL R&         ' Number of rows in the 2D-array
        LOCAL i&,j&
        C = 6            ' Number of columns in 2D-array is 6
        R = 5            ' Number of rows in 2D-array is 5
        CE = 3           ' There are 3 extra columns
        RE = 4           ' There are 4 extra rows
        ' Dimension the arrays with extra space
        DIM a(C+CE,R+RE) AS STRING
        REDIM RI(R+RE) : REDIM CI(C+CE)
        '
        t = ""
        CALL  MakeArray(a(),C,R)
        t = t + "Delete column 2 (zero based)"+$CRLF
        CALL DeleteColumn(a(),2,C,R)
        CALL ShowResults(a(),C,R)
        '
        t = ""
        CALL  MakeArray(a(),C,R)
        t = t + "Delete row 1 (zero based)"+$CRLF
        CALL DeleteRow(a(),1,C,R)
        CALL ShowResults(a(),C,R)
        '
        t = ""
        CALL  MakeArray(a(),C,R)
        t = t + "Insert column 3 (zero based)"+$CRLF
        CALL InsertColumn(a(),3,C,R)
        CALL ShowResults(a(),C,R)
        '
        t = ""
        CALL  MakeArray(a(),C,R)
        t = t + "Insert row 2 (zero based)"+$CRLF
        CALL InsertRow(a(),2,C,R)
        CALL ShowResults(a(),C,R)
        '
    END FUNCTION
    '
    SUB MakeArray(BYREF a() AS STRING,BYVAL C AS LONG, BYVAL R AS LONG)
        LOCAL i&,j&
        RANDOMIZE 1.3 ' Ensures same sequence of numbers at each call.
        FOR j = 0 TO R+RE ' Rows
            FOR i = 0 TO C+CE ' Columns
                ' The following definition of row and column index allows
                ' redim preserve with addition of extra rows of data,
                ' i.e. column index is first, row index is second.
                IF j<=R AND i<=C THEN ' initialize with data
                    a(i,j) = RIGHT$("    "+STR$(RND(0,9)),4)
                ELSE                  ' initialize with empty space symbolized by "---"
                    a(i,j) = "  ---"
                END IF
                t=t+a(i,j)
            NEXT
            t=t+$CRLF
        NEXT
        t=t+$CRLF
        ' Initialize row and column indices
        FOR j = 0 TO R+RE
            RI(j) = j
        NEXT
        FOR i = 0 TO C+CE
            CI(i) = i
        NEXT
    END SUB
    '
    SUB ShowResults(BYREF a() AS STRING,BYVAL C AS LONG, BYVAL R AS LONG)
        ' Show total array with extra space.
        LOCAL i&,j&
        FOR j = 0 TO R+RE
            FOR i = 0 TO C+CE
                t=t+a(CI(i),RI(j))
            NEXT
            t=t+$CRLF
        NEXT
        MSGBOX t
    END SUB
    '
    ' In the following routines you may blank the unused data. You may also
    ' keep them outside the region of interest. The latter option allows you to
    ' perform UNDO operations later if you regret. As the routines now stand,
    ' no data are actually being blanked. Only the indices determining the order
    ' of presentation are being manipulated. If you wants to blank data,
    ' you should uncomment the statements now commented.
    SUB DeleteColumn(BYREF a() AS STRING, BYVAL k AS LONG,BYVAL C AS LONG, BYVAL R AS LONG)
        LOCAL i AS LONG
       ' FOR i=0 TO R+RE
       '     a(CI(k),RI(i)) = ""
       ' NEXT
        i=CI(k) : ARRAY DELETE CI(k) FOR (C-k+CE+1) : CI(C+CE) = i
    END SUB
    '
    SUB InsertColumn(BYREF a() AS STRING, BYVAL k AS LONG,BYVAL C AS LONG, BYVAL R AS LONG)
        LOCAL i AS LONG
       ' FOR i=0 TO R+RE
       '     a(CI(C),RI(i)) = ""
       ' NEXT
        i = CI(C+CE) : ARRAY INSERT CI(k) FOR (C-k+CE+1) : CI(k) = i
    END SUB
    '
    SUB DeleteRow(BYREF a() AS STRING, BYVAL k AS LONG,BYVAL C AS LONG, BYVAL R AS LONG)
        LOCAL i AS LONG
       ' FOR i=0 TO C+CE
       '     a(CI(i),RI(k)) = ""
       ' NEXT
        i = RI(k) : ARRAY DELETE RI(k) FOR (R-k+RE+1) : RI(R+RE) = i
    END SUB
    '
    SUB InsertRow(BYREF a() AS STRING, BYVAL k AS LONG,BYVAL C AS LONG, BYVAL R AS LONG)
        LOCAL i AS LONG
       ' FOR i=0 TO C+CE
       '     a(CI(i),RI(R)) = ""
       ' NEXT
        i = RI(R+RE) : ARRAY INSERT RI(k) FOR (R-k+RE+1) : RI(k) = i
    END SUB

    [This message has been edited by Erik Christensen (edited October 13, 2002).]

    Comment

    Working...
    X