![]() |
|
Preserving multi-dim arrays - 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: Preserving multi-dim arrays (/showthread.php?tid=4226) |
Preserving multi-dim arrays - Pete - 12-18-2025 Since _Preserve is said to only work with single-dim arrays, what do you guys think about this method for working with multi-dim arrays when changing the number of array elements? Code: (Select All)
@Dimster This is basically how I handled it before _Preserve came along for single and multi-dim arrays. If you have put together other methods, it might be interesting to compare them here. Pete RE: Preserving multi-dim arrays - SMcNeill - 12-18-2025 Isn't the simplest way for multi-dim arrays: Make a new array the size you want. Copy over the data you want to preserve. Resize old array Copy Data back to old array (one single memcopy now, as the arrays are the same size and have the same indexes) Delete/Clear new array. It's not going to be quick, but it'll get the job done. And if you're resizing mutli-dimensional arrays constantly, you're doing something wrong. RE: Preserving multi-dim arrays - Pete - 12-18-2025 That's what my demo does, Steve. As an alternative, I don't know if it would also be possible to do the same using _Mem. I would think that would be equally or more involved. I haven't worked up anything using that keyword, so I can't provide an example. BTW - I hope the X-mas tree business is booming this year. I'd cut off my right arm to visit and help you with the trees, but then you'd be a little short handed. MAGA Christmas! Pete
RE: Preserving multi-dim arrays - Dimster - 12-18-2025 Pete that's very similar to what I've been doing with Multidim arrays. I like the ability to capture multi characteristics of a specific item like Name, Age, Height and Weight but I don't often find I need to expand the array like adding Eye Color to the characteristics. But when I do, I find I do something more ham handed then either your code or Steve's. What I do find I more often need to do with Multi D's is sorting and most often its sorting one of the characteristics and not the lead specific item. Like sort the Names by Age, or by Height or by Weight. Lot more involved with that exercise, the simplest way I found was to convert strings to numeric, make your lead specific item a decimal value and add up all the rest and then do a sort. For example, a multi D array with Names as the lead may already be in alphabetical order or not but if you substitute a number for the name you'd get the first name as 1, the 2nd as 2 and so on. Then you develop your sort array. Code: (Select All) Age = 29This example should have shown it was for SortArray(15) as the data was specifically for the 15th Name in the original multi D array. This is also the basis of how I (ham handedly) approach an expansion for Eye Color. The color has to be numeric. Rather than a SortArray, Its a Dim NewArray (1 to 5). and ReplacementArray (1 to 20, 1 to 20, 1 to 20, 1 to 20, 1 to 20) I use the same math formula with the exception of adding in the eye color (1=Blue, 2=Green,6=Brown etc as in Qbasic) so Name 15 in the NewArray now carries the value of Age+Weight+Height+Eye Color +(NameNum*.01). I then break all the values out from NewArray as I fill ReplacementArray. By keeping a copy of the NewArray I can sort things whenever I want. RE: Preserving multi-dim arrays - Pete - 12-18-2025 @Dimster Okay, but wouldn't something like this be easier? Code: (Select All)
Pete RE: Preserving multi-dim arrays - Dimster - 12-19-2025 Yes Pete, that method is easier for sorting string values which I assume the numeric values in your example are strings. But is the array a multi D? In fact, even if it isn't, it does a great job of using multi data elements. I like the use of numeric values for the accuracy of formula results, I haven't been able to trust string conversions to numeric values mostly because I'm often mis-converting and winding up with a zero value . for example I'll try something like Bob = val(Bob$) whereas Bob = 45 has a level of accuracy that I can depend on. But that being said, sorting is challenging and I like your method of a string array which captures all the elements of a multi D. Thanks for that. |