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

Just starting out? Need help? Post your questions and find answers here.
AnC
New User
New User
Posts: 3
Joined: Fri Mar 12, 2010 9:57 pm

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

Post 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:
srod
PureBasic Expert
PureBasic Expert
Posts: 10589
Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...

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

Post by srod »

Code: Select all

Global Dim A$(4)
:wink:

Either that or pass the array as a parameter to your procedure.
I may look like a mule, but I'm not a complete ass.
AnC
New User
New User
Posts: 3
Joined: Fri Mar 12, 2010 9:57 pm

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

Post by AnC »

Darn it, thanks srod, I never knew you could use Global Dim. :oops: as it worked in earlier versions of pb.
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8451
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

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

Post 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.
BERESHEIT
Post Reply