suggestion: initialize array values within the DIM statement - Printable Version +- QB64 Phoenix Edition (https://qb64phoenix.com/forum) +-- Forum: Chatting and Socializing (https://qb64phoenix.com/forum/forumdisplay.php?fid=11) +--- Forum: General Discussion (https://qb64phoenix.com/forum/forumdisplay.php?fid=2) +--- Thread: suggestion: initialize array values within the DIM statement (/showthread.php?tid=1315) Pages:
1
2
|
RE: suggestion: initialize array values within the DIM statement - Pete - 12-21-2022 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 those who like to start their arrays at zero... Code: (Select All) REDIM lNUMS(0) AS STRING: myray lNUMS(), "cow, pig, elephant, rhino, elephino" Pete RE: suggestion: initialize array values within the DIM statement - SpriggsySpriggs - 12-21-2022 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. RE: suggestion: initialize array values within the DIM statement - Kernelpanic - 12-21-2022 (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. RE: suggestion: initialize array values within the DIM statement - Pete - 12-21-2022 (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 RE: suggestion: initialize array values within the DIM statement - Kernelpanic - 12-21-2022 There is an easy way to set the lower index value in Basic: Option Base 1 RE: suggestion: initialize array values within the DIM statement - Pete - 12-21-2022 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. Pete RE: suggestion: initialize array values within the DIM statement - Kernelpanic - 12-21-2022 Here is the high school crap! For example Option Base 1: PS: So it is clearer: Code: (Select All) 'Option Base Beispiel - 21. Dez. 2022 RE: suggestion: initialize array values within the DIM statement - mnrvovrfc - 12-21-2022 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. RE: suggestion: initialize array values within the DIM statement - Pete - 12-22-2022 @Kernelpanic Yes, but try and explain the 'need' for it in that demo. Take it out, and it works exactly the same. Pete RE: suggestion: initialize array values within the DIM statement - Kernelpanic - 12-22-2022 (12-22-2022, 12:16 AM)Pete Wrote: @Kernelpanic 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! |