Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
suggestion: initialize array values within the DIM statement
#3
Could code a subprogram that takes two arguments: a string which is the list of values to initialize, and a dynamic array (which will be changed) to take the individual values from the list. Decide what will be the delimeter of the list. For mine I chose semicolon. Must also decide which type, even if it's integer. Sadly, for general purpose must choose "_INTEGER64".

The advantage of this is that the programmer doesn't have to worry about the array's size. The trade-off is that it's difficult this way to work with static arrays. It's clunky sometimes working with strings and having to parse them, but there's no other way to support an user's subprogram which accepts a number of parameters unknown to the compiler, something like the fabled "printf()" function in C runtime library.

Wait a moment -- @bobalooie why do you want to initialize an array with subscript zero as the first element? :O

I was hoping SMcNeill would come over to show off one of his functions... but here is my attempt.

Code: (Select All)
redim l(1 to 1) as integer
dim i as integer

fillinteger "1;2;5;8;4000", l()
for i = 1 to ubound(l)
    print i, l(i)
next
end

sub fillinteger (slist as string, li() as integer)
    static as long u, v, k
    if slist = "" then exit sub
    u = 1
    v = instr(slist, ";")
    k = 0
    do while v > 0
        k = k + 1
        redim _preserve li(1 to k) as integer
        li(k) = val(mid$(slist, u, v - u))
        u = v + 1
        v = instr(u + 1, slist, ";")
    loop
    k = k + 1
    redim _preserve li(1 to k) as integer
    li(k) = val(mid$(slist, u))
end sub
Reply


Messages In This Thread
RE: suggestion: initialize array values within the DIM statement - by mnrvovrfc - 12-21-2022, 03:59 PM



Users browsing this thread: 1 Guest(s)