xsharp.eu • ALEN()
Page 1 of 1

ALEN()

Posted: Thu Feb 20, 2020 5:05 pm
by kevclark64
I noticed that if you have an array of dimension 5,2 then ALEN(array) in Foxpro would return 10 but ALEN(array) in X# returns 5. Foxpro has an extra parameter to ALEN to return rows or columns, which X# does not support currently. However passing just the array gives the total elements across all dimensions in Foxpro. The FoxPro function list mentions ALEN as a function which works but not exactly with the same parameters. But it seems like if the same parameter is passed then it would be good to get back the same result.

ALEN()

Posted: Fri Feb 21, 2020 1:20 am
by Chris
Thanks Kevin for the report, I think we'll need to declare separate VO and VFP versions of the function to make it work for both dialects.

ALEN()

Posted: Fri Feb 21, 2020 7:16 am
by robert
Kevin,

The potential problem that I see with FoxPro arrays is that they are not really multi dimensional, but they emulate multi dimensions. Consider the following code

Code: Select all

DIMENSION myarray(5,2)
? ALEN(myarray)
myarray[1] = 10    && this sets element [1,1] 
myarray[1,2] = 12 
? myarray[1,1]    && 10
? myarray[2]     && 12, so this reads element [1,2]
FOR i = 1 TO 10
	? i, myarray[i]
	myarray[i] = i
NEXT
* you can even access undefined columns and rows
* in other xbase dialec ts all of the following would crash
? myarray[2,4]  && 6 ?
? myarray[3,3]  && 7 ? 
? myarray[9]     && 9 ?
In most of the other dialects this mixing of single and multi dimensional approach will not be possible.
When you declare an array as [5,2] then the first element of the array is a sub array of 2 elements.
Assigning 10 to it would remove the sub array in the first element and accessing it with a 1,1 index later would cause an exception because the sub array has been replaced with a single numeric value.
And in other XBase dialects accessing element myarray[2] would return an array of 2 elements and not a single value.
I am sure that internally FoxPro uses single dimensional arrays always and it somehow calculates the position in this array by calculating a single dimensional offset in the array from the dimensions used.
And FoxPro even allows to use positions outside of what has been defined !

I personally think this was a design flaw in FoxPro. An array that is declared as multi dimensional should not allow to be treated as single dimensional and accessing 'undefined' positions show throw an exception.


Robert

ALEN()

Posted: Wed Feb 26, 2020 1:05 pm
by mainhatten
robert wrote:I am sure that internally FoxPro uses single dimensional arrays always and it somehow calculates the position in this array by calculating a single dimensional offset in the array from the dimensions used.
True and it will bite you if you ReDim without first saving contents to re-populate redimensioned array.
I personally think this was a design flaw in FoxPro. An array that is declared as multi dimensional should not allow to be treated as single dimensional and accessing 'undefined' positions show throw an exception.
Array functionality was added early in the DOS years - and Fox made certain old programs will run in new versions. That kept some code p[w]arts alive...As in xSharp typed arrays should be the target to aim for, relegating such crazy behaviour to USUAL datatype hopefully will eliminate it from user code in time.

my 0.002€
thomas