Posted: 14th Jun 2007 16:33
I was wondering if there is any way to ReDim an array (eg. add more elements without losing the ones already there). I could, of course go the hard way and do this:

+ Code Snippet
dim array(3)
array(1)=2
array(2)=7
array(3)=1

dim tmparray(6)
for i=1 to array count(array(0))
  tmparray(i)=array(i)
next i

dim array(6)
for i=1 to array count(tmparray(0))
  array(i)=tmparray(i)
next i


or something like that, but I was wondering if there's an easier way.
Posted: 14th Jun 2007 16:39
Just dim the array again. It still keeps its contents.

+ Code Snippet
dim array(3)
array(1)=2
array(2)=7
array(3)=1

dim array(4)
array(4)= 100

for k =1 to 4
	print str$(k) + " = " + str$(array(k))
next k
wait key
Posted: 14th Jun 2007 17:12
nice! thank you!
Posted: 14th Jun 2007 17:42
Aren't all arrays kind of dynamic then?
Posted: 14th Jun 2007 17:49
Kind of. I believe what really happens behind the scenes is that a new array is created in the new size and all data is copied over. Not the fastest thing in the world, and not strictly dynamic. It works though.

I never dynamically redim arrays larger than 1 dimension. It's too slow. Plus, I think it starts losing data when you have too many dimensions.
Posted: 14th Jun 2007 17:51
Yeah I'd call them dynamic arrays, to do it properly read this from the help file, it sure beats messing with pointers ...

+ Code Snippet
                           Lists, Stacks and Queues
 
The unified array system is a remarkable method of managing your data with the minimum of fuss. Rather than maintaining different 
kinds of data storage, the unified array system combines the best features of all standard data structure management into a single 
method of access. The upshot of U.A.S is that you can initially start access of your data from an array, then decide to treat it as a 
stack, then decide to access it as a queue, and then back to an array. All this without loosing your data and without the need to 
manage its size or contents.
 
ARRAYS
Simple arrays form the basic foundation of the U.A.S system. Create an initial dynamic multidimensional array and access it through 
subscripts.
 
LISTS
Similar to an array, a list can expand and shrink based on the number of items contained. A list can theoretically have no items of data 
contained within. In contrast, it can store as much data as your memory availability allows. Accessing data through lists allows faster 
access than arrays in that there is no need to skip redundant items within the data. Items added and removed from a list are done so 
efficiently, removing the need for large clunky array sorting. You can also traverse a list without the need to know its size.
 
The list commands are:
EMPTY ARRAY
ARRAY INDEX TO BOTTOM
ARRAY INDEX TO TOP
ARRAY INSERT AT BOTTOM
ARRAY INSERT AT TOP
ARRAY INSERT AT ELEMENT
ARRAY DELETE ELEMENT
NEXT ARRAY INDEX
PREVIOUS ARRAY INDEX
ARRAY COUNT()
ARRAY INDEX VALID()
 
STACKS
A stack is summed up by the term 'last on, first off'. Stacks collect data added to it in a linear sequence of items, and the data is 
removed in the reverse order in which the items where added. Stacks are good for storing data prior to accessing a recursive function, 
or building time based data. It is a simple mechanism which removes the need for subscript or index control.
 
The stack commands are:
ARRAY INDEX TO STACK
ADD TO STACK
REMOVE FROM STACK
 
QUEUES
A queue is summed up by the term 'first on, first off'. Queues collect data added to it in a linear sequence of items, and the data is 
removed in the order in which the items where added. Queues are good for buffering data for later processing, where the order 
sequence of data is important.
 
The queue commands are:
ARRAY INDEX TO QUEUE
ADD TO QUEUE
REMOVE FROM QUEUE
 
It is important to note that multidimensional arrays cannot be accessed by stack and queue commands. Use single dimension arrays for 
these type of commands.