change an array's dimensions
BlitzMax Forums/BlitzMax Programming/change an array's dimensions
| ||
How do I do this after an array's creation? |
| ||
You can't. You can only create new 1D arrays (via slicing and concatenation) that retain the original array's contents. |
| ||
Even if I don't need to retain the original array's contents? It's just that I have a single array, but I need to be able to change how large it is after I define it as a global variable. |
| ||
Arrays are objects, so you could create a global Object variable and then put in it any kind of array you like. The only problem is you'd need to cast it every time you use it, and that adds opportunity for error. |
| ||
global array:int[,] = new int[300,400] array = new int[100,50] local array2:int[,] = array [edit] you can change the size not the dimensions. |
| ||
SuperStrict Global array:Object array = New Int[5] Local one_d:Int[] = Int[](array) one_d[3] = 9 Print one_d[3] array = New Float[4, 4] Local two_d:Float[,] = Float[,](array) two_d[2, 1] = 3.11 Print two_d[2,1] |
| ||
Thanks! |
| ||
Wouldn't it be better to have a multi-dimensional array of several dimensions, but with the last dimensions just set to size 0 until they are needed? |
| ||
How many dimesions are you using? There are a number a effective ways to store 2d arrays into 1d arrays instead. Just wondered, because then you wouldn't have all this resizing problem. |
| ||
Storing arrays within arrays allows you to nest them to any depth of hierarchy you like, and thus expand them to any number of dimensions after creation. It's just that blitz won't know that you have set things up as a multimensional array so you wont be able to use index[a,b,c,d,e,f] etc to reference elements. |
| ||
The issue was just defining a global 1D array variable to use as an array, then actually setting its size inside a function. |
| ||
Why do you need to do this? That doesn't sound like good design; surely there is a better way? |
| ||
Madk: I guess I'm confused then. As Nilium said, if you just have a single dimension array then you can resize it anyway you see fit with slices... Example: 'Set Global Variable Array (1D) Global a:Int[5] Local x:Int=0 'Function to reset size of array... Function dome(x:Int) a=a[..x] End Function Print Len(a) x=50 dome(x) Print Len(a) Is this what your trying to do???? Look in the BMax help under Slices for more examples... You can even create your own fixed size queue using this technique quite easily... Example follows: (Edit: I used the word stack, but stack isn't quite the correct term, this is more a queue, first in first out. I edited all references to stack...) Strict Local a:Int[]=[1,2,3,4,5] For Local i:Int=0 To 4 Print String(a[i]) Next Print "~n~n" a=a[1..] a=a[..5] a[4]=9 For Local i:Int=0 To 4 Print a[i] Next This is a very "basic" fixed size queue with (sort-of) FIFO function... :D Also, if you use Lists, then the size of the starting "array" is irrelevant... example: the following example is basically a queue, fifo, using lists instead of arrays. Strict Type tcrap Field x:Int Function Create:tcrap(xx:Int) Local t:tcrap=New tcrap t.x=xx Return t End Function End Type Global a:TList=CreateList() ListAddLast(a,tcrap.Create(1)) ListAddLast(a,tcrap.Create(2)) ListAddLast(a,tcrap.Create(3)) ListAddLast(a,tcrap.Create(4)) 'Local c:TLink=a.firstlink() Local c:TLink=New TLink For Local i:tcrap=EachIn a Print i.x Next Print "~n~n" c=a.firstlink() RemoveLink(c) For Local i:tcrap=EachIn a Print i.x Next Print "~n~n" ListAddLast(a,tcrap.Create(9)) For Local i:tcrap=EachIn a Print i.x Next Print "~n~n" c=a.firstlink() RemoveLink(c) For Local i:tcrap=EachIn a Print i.x Next :D |
| ||
a=a[..x] This was EXACTLY what I needed. Thank you. |
| ||
I think you confused an array's size and its dimensions, which is why most of our advice was unhelpful. [..x] is rather slow so you should avoid doing it often. It's usually better to resize an array by too much (and have some wasted/spare space) than to increase it a small amount several times. |