Page 1 of 1

How to redim array in a procedure

Posted: Wed Mar 25, 2009 3:32 pm
by Goos E
Hello,

I am trying to write a procedure witch inits a program. I am reading a array from a textfile. I want to use this array global in my program. my question is How to redim a array in a procedure while i use it global. With the next example i get a error named "Redim can't be used on a non-declared array:bmkust"

Code: Select all

procedure init()
do
 a$="1000"
 tel=tel+2
 redim bmkust(tel)
 bmkustx(tel)=val(a$)
until a$="1000"
endprocedure
global dim bmkust(1)
init()

Posted: Wed Mar 25, 2009 3:36 pm
by srod
Purebasic is a one-pass compiler... as a consequence you must position the Global statement before making first use of the array.

The same is true for any variables. If you use the global statement after the same variable appears in a function, PB will assume that the function variable is a local variable.

In short, put the Global statement at the top of your source.

Code: Select all

Global Dim bmkust(1) 

Procedure init() 
Repeat
 a$="1000" 
 tel=tel+2 
 ReDim bmkust(tel) 
 bmkust(tel)=Val(a$) 
Until a$="1000" 
EndProcedure 
init() 
(I took the liberty of removing the 'Do' statement which does not exist in Purebasic and of correcting a mis-spelled variable.)

Posted: Wed Mar 25, 2009 3:53 pm
by Goos E
Thank you very much that helped.