Resize arrays

Here we can discuss about the problem found
Post Reply
User avatar
Fernando Perez
Posts: 378
Joined: Mon Feb 15, 2021 10:09 pm
Location: Santander (Spain)
Has thanked: 195 times
Been thanked: 267 times

Resize arrays

Post by Fernando Perez »

I don't know if it's a bug or I'm not doing it right, but when I try to resize an array, it works for me if it has only one dimension, but not if it has more.

Code: [Local Link Removed for Guests]

wlog "array1:"
for i = 1 to 5
  dim array1(i)
  array1(i) = i
next i

for i = 0 to 5
  wlog array1(i)
next i    

wlog " "

' ------------------
wlog "array2:"
for i = 1 to 5
  for j = 1 to 5 
    dim array2(i, j)
    array2(i, j) = i
  next j
next i

for i = 0 to 5
  wlog "----"; i
  for j = 0 to 5
    wlog array2(i, j)
  next j
next i
User avatar
cicciocb
Site Admin
Posts: 2060
Joined: Mon Feb 03, 2020 1:15 pm
Location: Toulouse
Has thanked: 439 times
Been thanked: 1358 times
Contact:

Re: Resize arrays

Post by cicciocb »

I don't know exactly what do you expect from your "bizarre" code, probably to recover the old values in the new array?
Unfortunately this can work only with single dimension array as changing multiple dimensions the memory is allocated differently.
User avatar
Fernando Perez
Posts: 378
Joined: Mon Feb 15, 2021 10:09 pm
Location: Santander (Spain)
Has thanked: 195 times
Been thanked: 267 times

Re: Resize arrays

Post by Fernando Perez »

Perfect, that's what I wanted to know. Although it was what I suspected given the result of my code.
Well, nothing, perhaps it should be explained in the Help, since it only says:
<<The arrays can be re-sized using the same DIM command.
In this case all the existing elements will maintain the previous value except the new elements that will be initialized at 0 or null string. >>

And it may confuse you into thinking that it refers to all types of arrays.
User avatar
cicciocb
Site Admin
Posts: 2060
Joined: Mon Feb 03, 2020 1:15 pm
Location: Toulouse
Has thanked: 439 times
Been thanked: 1358 times
Contact:

Re: Resize arrays

Post by cicciocb »

As you know, the "dimensions" do not exists in memory as the "multiple dimensions" arrays simply points in block of linear memory. So, in case of single dimension, it is easy to maintain the old values but on multiple dimension, the interpreter should move and recompose the new array with the old values, dimension per dimension ... a big effort for a negligible need
User avatar
Fernando Perez
Posts: 378
Joined: Mon Feb 15, 2021 10:09 pm
Location: Santander (Spain)
Has thanked: 195 times
Been thanked: 267 times

Re: Resize arrays

Post by Fernando Perez »

I understand and I totally agree with you. It's simply that I'm investigating all the "nooks and crannies" I can of Annex.
In my case, I was trying to read a file line by line and add dimensions to the array that will contain it to adjust the memory to the minimum necessary.
But it is much easier to find out in advance how many lines the file has and then size the array accordingly.
Or do without arrays and work with file.read_iobuff.

Thank you.
Post Reply