Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Three-dimensional array
#17
(01-06-2023, 03:48 PM)Spriggsy Wrote: Pretty sure PRESERVE works with multiple dimension arrays. I think my SQL stuff that depends on a two-dimensional array wouldn't work if PRESERVE only handled one dimension arrays.

It works, but only on one dimension.

REDIM x(10,10)

REDIM x(10,20) <-- this is fine.

REDIM x(20,10) <-- this will corrupt your index.

The reason is real simple -- redim just copies the existing mem block and moves it to a new memblock, and data is stored in memory as a 1d block of memory.

For example:

12
34
56
78

The above is a 2 x 4 array of single digits.   In memory, it's stored simply as "12345678". Left to right, top to bottom.

Now, redim and add a column, you get:

123
456
780
000

Corrupted data!  Sure, you added 4 bytes to the whole array, but when you moved that "12345678" over with those 4 new "0000" that you just created, they map to the index in the manner above.

So, why can you create a new row with no problems??

12
34
56
78
00

See where your "12345678" copied over and remained in the same index positions?  Those two new "0" characters are at the end of the data, but the structure of the data itself hasn't changed.   We still read it left-to-right, top-to-bottom.

Multi-dimensional arrays are only redim _preserve-able with the last element.  Anything else, you need to move manually into a new array yourself.
Reply


Messages In This Thread
Three-dimensional array - by Kernelpanic - 01-03-2023, 06:39 PM
RE: Three-dimensional array - by bplus - 01-03-2023, 07:38 PM
RE: Three-dimensional array - by SpriggsySpriggs - 01-03-2023, 07:40 PM
RE: Three-dimensional array - by mnrvovrfc - 01-03-2023, 07:46 PM
RE: Three-dimensional array - by SMcNeill - 01-03-2023, 07:56 PM
RE: Three-dimensional array - by TempodiBasic - 01-03-2023, 11:13 PM
RE: Three-dimensional array - by Kernelpanic - 01-03-2023, 08:36 PM
RE: Three-dimensional array - by TempodiBasic - 01-03-2023, 11:07 PM
RE: Three-dimensional array - by Kernelpanic - 01-04-2023, 10:12 PM
RE: Three-dimensional array - by SMcNeill - 01-04-2023, 10:57 PM
RE: Three-dimensional array - by TerryRitchie - 01-04-2023, 11:41 PM
RE: Three-dimensional array - by Kernelpanic - 01-06-2023, 03:20 PM
RE: Three-dimensional array - by TempodiBasic - 01-05-2023, 09:07 PM
RE: Three-dimensional array - by mnrvovrfc - 01-05-2023, 10:07 PM
RE: Three-dimensional array - by TerryRitchie - 01-05-2023, 10:04 PM
RE: Three-dimensional array - by SpriggsySpriggs - 01-06-2023, 03:48 PM
RE: Three-dimensional array - by SMcNeill - 01-07-2023, 12:17 AM
RE: Three-dimensional array - by James D Jarvis - 01-07-2023, 04:53 AM
RE: Three-dimensional array - by mnrvovrfc - 01-07-2023, 06:41 AM
RE: Three-dimensional array - by TempodiBasic - 01-07-2023, 11:55 AM
RE: Three-dimensional array - by TempodiBasic - 01-08-2023, 05:39 PM



Users browsing this thread: 2 Guest(s)