Variable Manipulation

New to FreeBASIC? Post your questions here.
Post Reply
TurtleProgrammer
Posts: 37
Joined: Jan 26, 2017 7:54

Variable Manipulation

Post by TurtleProgrammer »

In the example below I want to read each integer from the DATA statement into each p[variable#] so I can change certain variables at certain instances. This will save me time from having to input each integer into each variable my self.

Code: Select all

'DIM's for the variables.
dim p1, p2, p3, p4, p5, p6, p7, p8, p9, p10 as integer

'Each of the following variables needs to hold the next consecutive integer from the DATA statement.
'Ex. p1 = 1, p2 = 2, p3 = 3, and so on.

p1 =
p2 =
p3 =
p4 =
p5 =
p6 =
p7 =
p8 =
p9 =
p10 =




data 1,2,3,5,7,3,2,1,5,3
D.J.Peters
Posts: 8586
Joined: May 28, 2005 3:28
Contact:

Re: Variable Manipulation

Post by D.J.Peters »

Code: Select all

data 1,2,3,5,7,3,2,1,5,3

dim as integer p1, p2, p3, p4, p5, p6, p7, p8, p9, p10

read p1,p2,p3,p4,p5,p6,p7,p8,p9,p10

print p1,p2,p3,p4,p5,p6,p7,p8,p9,p10

sleep
PaulSquires
Posts: 999
Joined: Jul 14, 2005 23:41

Re: Variable Manipulation

Post by PaulSquires »

In addition to Joshy's code solution, you might want to use an array rather than have to create separate variable names for each DATA element.

Code: Select all

data 1,2,3,5,7,3,2,1,5,3

dim as integer p(1 to 10)

for i as long = 1 to 10
   read p(i)
next

for i as long = 1 to 10
   print p(i);
next

sleep
Munair
Posts: 1286
Joined: Oct 19, 2017 15:00
Location: Netherlands
Contact:

Re: Variable Manipulation

Post by Munair »

It is also a good habit to use labels with data blocks. It makes the code easier to read and you already have an identifier in case more data blocks are added.

Code: Select all

dim as integer p(1 to 10)
restore MyData
for i as long = 1 to 10
   read p(i)
next

for i as long = 1 to 10
   print p(i);
next

sleep
end

MyData:
data 1,2,3,5,7,3,2,1,5,3
Post Reply