Why I cannot REDIM?

Just starting out? Need help? Post your questions and find answers here.
avatar
User
User
Posts: 35
Joined: Fri Apr 16, 2010 11:43 am

Why I cannot REDIM?

Post by avatar »

Please help. I do not understand why I cannot REDIM my array

Code: Select all

Dim GV.l(1,2)
GV(0,0) = 100
GV(0,1) = 200
GV(1,0) = 300
GV(1,1) = 400
ReDim GV(2,2) ; <------------------ ERROR
GV(2,0) = 1100
GV(2,1) = 1200
For Count = 0 To ArraySize(GV())
  Debug Str(GV(Count,0))+","+Str(GV(Count,1))
Next
Error log is :
[13:35:23] Waiting for executable to start...
[13:35:23] Executable type: Windows - x86 (32bit)
[13:35:23] Executable started.
[13:35:23] The Program execution has finished.
[13:36:59] Waiting for executable to start...
[13:36:59] Executable type: Windows - x86 (32bit)
[13:36:59] Executable started.
[13:36:59] [ERROR] test.pbi (Line: 6)
[13:36:59] [ERROR] Only the last dimension of an array can be changed with ReDim.
[13:37:03] The Program was killed.
User avatar
idle
Always Here
Always Here
Posts: 5984
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: Why I cannot REDIM?

Post by idle »

That would be because your re-dimming the 1st dimension
Only the last dimension of an array can be changed with ReDim.
avatar
User
User
Posts: 35
Joined: Fri Apr 16, 2010 11:43 am

Re: Why I cannot REDIM?

Post by avatar »

Not allow to REDIM the 1st dimension?
What will be the alternative to increase the dimension size?
User avatar
idle
Always Here
Always Here
Posts: 5984
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: Why I cannot REDIM?

Post by idle »

In the specific case you just need to flip your dim to (2,1) and redim the second dimension
Sorry I can't be of much more help.
avatar
User
User
Posts: 35
Joined: Fri Apr 16, 2010 11:43 am

Re: Why I cannot REDIM?

Post by avatar »

Thanks idle.
You are already very helpful.
May be I consider to better change to use structure list.
User avatar
Kaeru Gaman
Addict
Addict
Posts: 4826
Joined: Sun Mar 19, 2006 1:57 pm
Location: Germany

Re: Why I cannot REDIM?

Post by Kaeru Gaman »

if you don't need to keep the content, just use Dim again.
the content will be erased, but you can use ANY dimensions you like.
oh... and have a nice day.
User avatar
charvista
Addict
Addict
Posts: 949
Joined: Tue Sep 23, 2008 11:38 pm
Location: Belgium

Re: Why I cannot REDIM?

Post by charvista »

Hi avatar,
This way you can ReDim your Array of type Long, with two dimensions (L2). Somewhat ugly coding, but it works.... and you keep the original GV.l() array.

Hope it helps.

Code: Select all

Procedure zCopyArrayL2(Array FromA.l(2), Array ToA.l(2))
    For i=0 To ArraySize(FromA(),1)
        For j=0 To ArraySize(FromA(),2)
            ToA(i,j)=FromA(i,j)
        Next j
    Next i
EndProcedure    

Dim GV.l(1,1)
GV(0,0) = 100
GV(0,1) = 200
GV(1,0) = 300
GV(1,1) = 400

;ReDim GV(2,2) ; <------------------ ERROR

Dim NewGV.l(2,2)
zCopyArrayL2(GV.l(),NewGV.l())
Dim GV.l(2,2)
zCopyArrayL2(NewGV.l(),GV.l())
Dim NewGV(0,0) ; reduce to a minimum to save memory

GV(2,0) = 1100
GV(2,1) = 1200
For Count = 0 To ArraySize(GV())
  Debug Str(GV(Count,0))+","+Str(GV(Count,1))
Next
- Windows 11 Home 64-bit
- PureBasic 6.10 LTS (x64)
- 64 Gb RAM
- 13th Gen Intel(R) Core(TM) i9-13900K 3.00 GHz
- 5K monitor with DPI @ 200%
Post Reply