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
|
suggestion: initialize array values within the DIM statement - bobalooie - 12-21-2022 How difficult would it be to implement something similar to this in QB64PE? OPTION _EXPLICIT DIM AS LONG LNUMS(5) = {1, 2, 5, 8, 4000} This is syntax similar to C, and it is also supported to an extent by FreeBASIC. I have used this construct in FreeBASIC, and I have found it useful when initializing short arrays of 'fixed' data in a program. (I always use OPTION _EXPLICIT, it has saved me more than a few times from fat-fingered typing.) RE: suggestion: initialize array values within the DIM statement - Pete - 12-21-2022 I suppose there are a couple of ways it could be coded into a function... Code: (Select All) DIM AS LONG LNUMS(5) I suppose it all depends on finding a dev who could use it frequently enough to include it in a future release. Pete RE: suggestion: initialize array values within the DIM statement - mnrvovrfc - 12-21-2022 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 RE: suggestion: initialize array values within the DIM statement - SpriggsySpriggs - 12-21-2022 @bobalooie I, too, am an advocate for OPTION _EXPLICIT usage. If only it had been used in the QB64 source code. RE: suggestion: initialize array values within the DIM statement - bobalooie - 12-21-2022 (12-21-2022, 03:59 PM)mnrvovrfc Wrote: Wait a moment -- @bobalooie why do you want to initialize an array with subscript zero as the first element? :O Well, in this case I am interested in just iterating through the array as a lookup table of sorts, so it doesn't really matter if the first element is at 0 or 1. If I was working in C, I could use the syntax I showed or perhaps make a doubly-linked list so I could iterate either direction. Your example works well, but I perceive that I would need a new fill function for every data type I want to store. Not really a challenge of course, except for a list of CONST values. Soldiering on. RE: suggestion: initialize array values within the DIM statement - bobalooie - 12-21-2022 (12-21-2022, 05:23 AM)Pete Wrote: I suppose there are a couple of ways it could be coded into a function... Yup, both good approaches. I'm just trying to be lazy and not have to type all that extra code. RE: suggestion: initialize array values within the DIM statement - madscijr - 12-21-2022 (12-21-2022, 03:36 AM)bobalooie Wrote: How difficult would it be to implement something similar to this in QB64PE? This would definitely be a nice feature - a function like what @mnrvovrfc (how do you pronounce that anyway?!) should work. When I am at my PC, I'll give it a try. Thanks mnrvovrfc! RE: suggestion: initialize array values within the DIM statement - SMcNeill - 12-21-2022 (12-21-2022, 03:59 PM)mnrvovrfc Wrote: I was hoping SMcNeill would come over to show off one of his functions... but here is my attempt. My way of doing this type of thing is really, really simple: Code: (Select All) DIM foo(10) AS INTEGER The label and restore aren't required in the code above, but it's just one of them things that I've gotten into the habit of always using. I *NEVER* do a READ with my DATA, without doing a RESTORE first. I do too much work with library files and other such things, so I never go under the assumption that the data I want to read is necessarily the first data in the program (or the next data in the program). Restore always puts you at the data you want, and I'm just in the habit of always using it. Skip it, if you don't think you need it for your own uses. A lot simpler than a function and parsing a string and all that complicated stuff. RE: suggestion: initialize array values within the DIM statement - Pete - 12-21-2022 I liked mn's example. Well, I don't see a function, because we are returning a set of array values, not just a single value; so a sub seems a more likely choice... Code: (Select All) REDIM LNUMS(0): myray LNUMS(), "1, 2, 5, 8, 4000" Now I think we need to restrict it to dynamic arrays, to avoid a duplicate definition. Maybe this could be expanded on to handle string arrays, too. As far as how to define them by type, single, long, etc. That's more complicated. Edit: Steve was posting while I was writing. I se the same method.In fact, just recently in my replies to the password thread. Pete RE: suggestion: initialize array values within the DIM statement - mnrvovrfc - 12-21-2022 (12-21-2022, 07:01 PM)Pete Wrote: Well, I don't see a function, because we are returning a set of array values, not just a single value; so a sub seems a more likely choice... You should do your loop from 0 to 4, not 1 to 5 because you started an array at subscript zero... Also the subprogram that you wrote doesn't initialize the array at subscript zero, it just increases local "i" by one. Then its value is one at first pass. Even better (in main program) do: Code: (Select All) FOR i = LBOUND(LNUMS) to UBOUND(LNUMS) |