PDA

View Full Version : Array filling (2d)


Donone
05-11-2007, 04:52 PM
I have seen a past post on filling an array, but it only covers a single dimension.
I have tried...
SDim(a$,TSHORT,12, 2);
Fill(a$,[1,2],[3,4],[5,6]...); which does not work and can't figure an alternative that does.
Can you help please?

PointOfLight
05-11-2007, 05:33 PM
I could be wrong, and Alain will correct me if I am, but I think Fill only works with single dimension arrays.* To populate multiple dimensions, you'll have to access each element directly.

kornalius
05-11-2007, 05:37 PM
It is not possible.

You can try filling the array, the following way:

Fill(a$, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11);

There is actually a problem with the Fill() function in the latest beta. It is fixed. It will be out next week.

Donone
05-11-2007, 05:40 PM
Thank you PointOfLight.
A bit longwinded with a large array. Perhaps the future might bring...
a$=[[1,2],[3,4],[5,6]....] or better...
a$=[[1,2][3,4][5,6]...]
:) :)

Donone
05-11-2007, 05:44 PM
Thank you Kornalius. I'll give it a try but it doesn't (on the surface) look as if it will access the way it should using two indexes, but one could use a single and step twice. I'll see how it goes.

Donone
05-11-2007, 05:59 PM
I guess the solution will be an array of structures, each structure being the two field array.
At least I know not to waste time trying the original.

[Edit]
Interestingly, although Fill doesn't yet work in 1.30 (but you have fixed it), this does...
a$=[1,2,3,4,5,6,7,8,9];

PointOfLight
05-11-2007, 06:07 PM
How are you populating the data? Is it coming from a file, or are you hard coding it in the application? If you are doing the latter, you might consider putting the data in a file, reading the contents of the file and then just doing a loop and populating the data that way. Quick, untested example:


Data File:
1,2,3,4,...

Dim(arr$, 12, 2);

f$ = fopen("filename", "r");
s$ = ReadString(f$);
fclose(f$);

StrToList(s$, ",", &data$);
i$ = 1;
j$ = 1;

foreach(data$)
arr$[i$,j$] = data$;
i$++;
if(i$ == 13)
i$ = 1;
j$++;
end;

Donone
05-11-2007, 06:21 PM
Thanks PointOfLight. I have by now realised that an array of structures poses the same problem, that of populating.
I am hand coding and though it would work from a file, I will leave it as a last resort to add extra files. Thank you for your suggestion.
I just edited my post to show that a$=[1,2,3,4...] works without using Fill. (This temporarily gets around the short term 1.30 bug with Fill.)
It doesn't solve the populating problem of 2 dimensions so I will alter the program to double step a single array, it is a minor change.
If it gets bigger I will resort to a file.

kornalius
05-12-2007, 02:12 AM
You can try a matrix which supports multi-dimension.

a$ = [[1,2],[1,2],[2,3]];

ShowMessage(a$[0,0]);

Donone
05-12-2007, 04:35 PM
Thank you Kornalius, I don't know why I didn't pick that up. That should solve everything.

Donone
05-12-2007, 05:25 PM
Kornalius, I have just tried the Matrix but have hit a problem.
1.30 beta 3 Pro
I have...
a$=[[12,13],[14,15]];
ShowMessage(a$[0,1]);

I get a runtime error at ShowMessage...
"Wrong number of offsets[2], for variable A$, 1 are required."
I got a similar message when experimenting with 2 dimensional arrays etc.
Will you please suggest what may be wrong.

kornalius
05-14-2007, 02:03 PM
You need to do the following:

ShowMessage(a$[0][1]);

Matrix are a little different to refer to than standard arrays.

You need to obtain the pointer of the first sub-element and then the offset of the element you want in it.

Donone
05-14-2007, 02:19 PM
Thank you very much Kornalius.
There is much to learn :)