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
If eggs are brain food, Biden takes his scrambled.
Reply
#12
I always dim all my arrays as MyArray(0 to 5) or whatever. I want 0 to be my first element and I want to be doubly sure that my array is the exact size that I'm wanting.
Ask me about Windows API and maybe some Linux stuff
Reply
#13
(12-21-2022, 09:37 PM)Spriggsy Wrote: I always dim all my arrays as MyArray(0 to 5) or whatever. I want 0 to be my first element and I want to be doubly sure that my array is the exact size that I'm wanting.

What about a program that lists the names of the months? Zero for January and eleven for December seems a bit confusing.

It would be just the way it is in C, but one do not necessarily have to imitate it in Basic.
Reply
#14
(12-21-2022, 09:37 PM)Spriggsy Wrote: I always dim all my arrays as MyArray(0 to 5) or whatever. I want 0 to be my first element and I want to be doubly sure that my array is the exact size that I'm wanting.

I'll have to admit I've never seen the need to do the '0 to whatever' part in naming an array.

DIM pete(100) is the same as DIM pete(0 TO 100)

Now Steve would say the problem we have here is 101 too many pete's to deal with, but I digress...

So what's the dif between these two ways to declare an array for this situation? I mean sure, for a different situation like DIM x(5 TO 10) I get, but DIM x(10) includes zero so it is the same as DIM x(0 TO 10).

pete
pete pete pete pete pete pete pete pete pete pete
pete pete pete pete pete pete pete pete pete pete
pete pete pete pete pete pete pete pete pete pete
pete pete pete pete pete pete pete pete pete pete
pete pete pete pete pete pete pete pete pete pete
pete pete pete pete pete pete pete pete pete pete
pete pete pete pete pete pete pete pete pete pete
pete pete pete pete pete pete pete pete pete pete
pete pete pete pete pete pete pete pete pete pete
pete pete pete pete pete pete pete pete pete pete
Reply
#15
There is an easy way to set the lower index value in Basic: Option Base 1  

[Image: icon-lol.gif]

Reply
#16
Yes, yes, we all know about OPTION BASE 1. That's pretty much high school crap. I'm trying to well get past that here... at least to OPTION BASE 3. Big Grin

Pete
Reply
#17
Here is the high school crap! For example Option Base 1:  Tongue

PS: So it is clearer:

Code: (Select All)
'Option Base Beispiel - 21. Dez. 2022

Option Base 1
Option _Explicit

Dim As String jahresMonate(12)
Dim As Integer monat

Data Januar,Februar,Maerz,April,Mai,Juni
Data Juli,August,September,Oktober,November,Dezember

For monat = 1 To 12
  Read jahresMonate(monat)
  Print Using "Monat ##. "; monat;
  Print jahresMonate(monat)
Next monat

End
Reply
#18
An extra element is being allocated. I know "(1 TO 1)" is extra typing but I prefer it.

This time I said nothing about "OPTION BASE" because the way I suggested in my code to declare an array makes "OPTION BASE" useless. Each one to his/her own.

What if you needed to use "_MEM" on that "ARRAY(0)" thing, gave nothing to that zeroth-subscript element and you expected the opposite? Instead you have to use "LEN" to figure out the size of UDT variable, and something remembered such as 8 for _INTEGER64, and give up that "_MEM-variable-dot-OFFSET" as your first element. If you create such an array where you think you have five elements and then "_MEMCOPY" only five elements, at offset zero, the "ARRAY(5)" becomes undefined.

Disregard that plea if you're hardcore about QuickBASIC and QBasic, and about QB64 before v0.95.
Reply
#19
@Kernelpanic

Yes, but try and explain the 'need' for it in that demo. Take it out, and it works exactly the same.

Pete
If eggs are brain food, Biden takes his scrambled.
Reply
#20
(12-22-2022, 12:16 AM)Pete Wrote: @Kernelpanic

Yes, but try and explain the 'need' for it in that demo. Take it out, and it works exactly the same.

Pete

That's true, many roads lead to Rome. - With Option Base it's like with Declare, I know immediately what's going on. Without that, I have to figure out what the person in question is doing by going through the source code. 
I just love my order!  Wink
Reply




Users browsing this thread: 7 Guest(s)