Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
suggestion: initialize array values within the DIM statement
#11
1) Personal preference. I hardly ever use zero in array routine. The reason, if I'm referencing the third value, I want to use array(3), not array (2).

2) These sub-routines only support one numerical variable type. Mine passes the array by reference, but that still requires a type declaration.

3) This method seems to be better suited for non-dimensioned string arrays, with the proper code conversion, of course.

Universal non-dim string example...
Code: (Select All)
REDIM lNUMS(0) AS STRING: myray lNUMS(), "cow, pig, elephant, rhino, elephino"

FOR i = 1 TO 5
    PRINT lNUMS(i)
NEXT

SUB myray (myarray() AS STRING, myarray_elements AS STRING)
    DO
        i = i + 1
        j = INSTR(seed, myarray_elements + ",", ",")
        IF j = 0 THEN EXIT DO
        REDIM _PRESERVE myarray(i)
        myarray(i) = MID$(myarray_elements, seed + 1, j - seed - 1)
        seed = j + 1
    LOOP
END SUB

For those who like to start their arrays at zero...

Code: (Select All)
REDIM lNUMS(0) AS STRING: myray lNUMS(), "cow, pig, elephant, rhino, elephino"

FOR i = 0 TO 4
    PRINT lNUMS(i)
NEXT

SUB myray (myarray() AS STRING, myarray_elements AS STRING)
    DO
        j = INSTR(seed, myarray_elements + ",", ",")
        IF j = 0 THEN EXIT DO
        REDIM _PRESERVE myarray(i)
        myarray(i) = MID$(myarray_elements, seed + 1, j - seed - 1)
        seed = j + 1: i = i + 1
    LOOP
END SUB

Pete
Shoot first and shoot people who ask questions, later.
Reply


Messages In This Thread
RE: suggestion: initialize array values within the DIM statement - by Pete - 12-21-2022, 08:02 PM



Users browsing this thread: 2 Guest(s)