Page 1 of 1

reading data inside a procedure - why will this not work?

Posted: Thu Aug 12, 2010 10:39 am
by AnC
Very new here so please be nice! I've used arrays before so I know i'm doing something right but...

I have a text file that I am reading strings from into an array which works fine.

But when I put code inside a procedure it complains about my arrary not being recognised.

Below is just a simple example of what I mean inside a procedure:

Code: Select all

Dim A$(4)

Procedure ReadIt()
  Restore StringData
    For R=0 To 4
      Read.s Dat$ : A$(R)=Dat$
      Debug (R)
    Next R
EndProcedure

ReadIt()

End  
  
DataSection

 StringData:
 Data.s "Hello", "This", "is ", "my","data"
EndDataSection
Now here it is outside the procedure which works fine:

Code: Select all

Dim A$(4)

Restore StringData
   For R=0 To 4
     Read.s Dat$ : A$(R)=Dat$
     Debug A$(R)
   Next R
End  
  
DataSection

 StringData:
 Data.s "Hello", "This", "is ", "my","data"
EndDataSection
I know i'm about to be embarassed here so I await your humbling words.. :oops:

Re: reading data inside a procedure - why will this not work

Posted: Thu Aug 12, 2010 10:41 am
by srod

Code: Select all

Global Dim A$(4)
:wink:

Either that or pass the array as a parameter to your procedure.

Re: reading data inside a procedure - why will this not work

Posted: Thu Aug 12, 2010 10:53 am
by AnC
Darn it, thanks srod, I never knew you could use Global Dim. :oops: as it worked in earlier versions of pb.

Re: reading data inside a procedure - why will this not work

Posted: Thu Aug 12, 2010 1:10 pm
by netmaestro
as it worked in earlier versions of pb.
Arrays and lists in Purebasic prior to v4.0 were global by default and you couldn't change that. As a result it's a common change you have to make when updating code from those versions.