Array copying inconsistency

Discussions related to database technologies, file handling, directories and storage
Richard Russell
Posts: 477
Joined: Tue 18 Jun 2024, 09:32

Array copying inconsistency

Post by Richard Russell »

I've just noticed a surprising (to me) inconsistency between my BASICs and Sophie's BASICs in respect of copying whole arrays. In mine this code works fine, and is useful if you want to change the 'shape' of an array without changing the data:

Code: Select all

DIM a(7), b(3,1)
a() = 1, 2, 3, 4, 5, 6, 7, 8
b() = a()
The two arrays have the same data type and the same size (8 elements in all) so they are compatible and the copy succeeds.

But in Acorn's ARM BASIC 5, and Matrix Brandy BASIC, the code fails with an error message that the arrays must have the same dimensions. Why? It seems to be an entirely unnecessary restriction.
DDRM
Posts: 22
Joined: Mon 17 Jun 2024, 08:02

Re: Array copying inconsistency

Post by DDRM »

I guess it was felt useful as a safety check - in general copying one array into another of different shape is not a good idea, though if you KNOW they have the same number of elements it can be useful, as you say. I guess it also depends on how the data is stored in memory - your method presupposes the elements are all in a continuous line, and that the array "knows" where to break it into rows. It would fail if the data were padded for some reason (as is often the case for images,for example).

FWIW, the numpy library in python allows you to reshape arrays, but you have to do it explicitly (and of course it will fail if the number of elements doesn't match).

Best wishes,

D
Richard Russell
Posts: 477
Joined: Tue 18 Jun 2024, 09:32

Re: Array copying inconsistency

Post by Richard Russell »

DDRM wrote: Mon 22 Sep 2025, 08:24 I guess it was felt useful as a safety check - in general copying one array into another of different shape is not a good idea
But what results is an error, not a warning. This is not a compiled language which could alert the programmer to something which is not in general "a good idea", thus requiring extra care to be taken, but an interpreted language which fails at run time, making it impossible to do it even if it is useful.

There are hundreds of things that you can do in BBC BASIC which can be dangerous, but that are sometimes useful and therefore contribute to the power of the language. For example the ability to write to an arbitrary memory address (indirection) or to call a machine-code routine at an arbitrary address.

Preventing you doing something which might be useful, on the grounds that it is not always "a good idea", is deeply contrary to the philosophy of BBC BASIC!
your method presupposes the elements are all in a continuous line
That's not true at all. If all the elements of an array are contiguous in memory (which they are guaranteed to be in BBC BASIC) it makes the copy easier because it can be a straight linear copy, but even if not the interpreter knows the exact layout of both source and destination arrays and could perfectly well perform the copy if not contiguous.

BBC BASIC already has a supported and documented means of copying a one dimensional list of constants into a two-dimensional array, which is surely no different fundamentally from what we are discussing. Would you argue that should not be allowed? :roll:

Code: Select all

DIM a(3,1)
a() = 1,2,3,4,5,6,7,8
(and of course it will fail if the number of elements doesn't match).
My BASICs fail with an error message if the total number of elements don't match.