Filling arrays

Discussion in 'Code Samples & Tips' started by Erel, Jan 9, 2008.

  1. Erel

    Erel Administrator Staff Member Licensed User

    It is sometimes necessary to fill arrays with some known values.
    The most straightforward method is:
    Code:
    array(0) = 2
    array(1) = 3
    ...
    For larger arrays this method is a little cumbersome.
    A much cleaner solution is to use StrSplit.
    StrSplit splits a string and returns an array.

    First we declare the array in sub Globals as an empty array.
    Later we load the data using StrSplit:
    Code:
    Sub Globals
        
    Dim data(0)
        
    Dim names(0)
    End Sub

    Sub App_Start
        data() = StrSplit(
    "2,3,5,7,11,13,17,19,23" , ",")
        names() = StrSplit(
    "John,Robert,Mike,Barbara" , ",")
        
        
    'Show all names
        For i = 0 To ArrayLen(names())-1
            
    Msgbox(names(i))
        
    Next
    End Sub
    Make sure that you don't leave any extra spaces inside the string.
     
  2. grgczyz

    grgczyz Member Licensed User

    Filling 2-D arrays ... is it possible?

    :sign0188:
    I like this method very much however I wonder if it is possible to fill 2-D arrays similarly as 1-D arrays?
     
  3. Erel

    Erel Administrator Staff Member Licensed User

    Currently not. Maybe in a future version.
     
  4. grgczyz

    grgczyz Member Licensed User

    It would be very useful ... thank you Erel :)
     
Loading...