Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Newcomers DBPro Corner / arrays and data

Author
Message
Guitarman
20
Years of Service
User Offline
Joined: 31st Jan 2004
Location:
Posted: 28th May 2005 12:31
Ok so I've got an interesting question...
is it possible to do the following:

DIM test(1,5)

test(1,1)=DATA 0,1,0,1
test(1,2)=DATA 1,0,1,0
test(1,3)=DATA 0,0,1,1
test(1,4)=DATA 1,1,0,0
test(1,5)=DATA 1,0,0,1

and then read each of these later? Oh yeah I guess it'd be important to explain that I do not completely understand the data command. I understand how one can:

Data 0,0,0,0,5
read a#,b#,c#,d#,e#
print a#
print b#
print c#
print d#
print e#

and the resulting output would be
0
0
0
0
5

but how do you use multiple data statements and read multiple data things?i.e.

DATA 0,1,5,7,9
DATA "John","SAM","Sue", "Jean"

how could you reacall both the numbers and the strings using the data command? Hope I've been clear and I haven't asked something that everyone asked (I did search the forum)

thanks
k
zenassem
21
Years of Service
User Offline
Joined: 10th Mar 2003
Location: Long Island, NY
Posted: 28th May 2005 13:10 Edited at: 28th May 2005 13:26
Quote: " Ok so I've got an interesting question...
is it possible to do the following:

DIM test(1,5)

test(1,1)=DATA 0,1,0,1
test(1,2)=DATA 1,0,1,0
test(1,3)=DATA 0,0,1,1
test(1,4)=DATA 1,1,0,0
test(1,5)=DATA 1,0,0,1
"


Answer: No. I think you may be confused about how arrays and data commands work.


dim test(1,5) (lets disregard that you actually have a 0 element)

each subscript pair <array(x,y)> can only hold ONE integer

so you could say...


test(1,1)= 1
test(1,2)= 0
test(1,3)= 500


to store the data you have above you would need to dim the array

dim test(4,5) (again I am disregarding element 0 to avoid confusion)

Then you could do the following:


for y=1 to 5
for x=1 to 4
test(x,y)=read num
next x
next y

data 0,1,0,1
data 1,0,1,0
data 0,0,1,1
data 1,1,0,0
data 1,0,0,1


part II

you can read multiple data statements in a few differnet ways. If the
strings and integers are related you could do this:

dim names$(4)
dim numbs(4)

for x=1 to 4
read name$: read num
print name$
print num
names$(x)=name$
numbs(x)=num
next x

data "John",0,"Sam",1,"Sue",7,"Jean",9


Also if you needed to read them seperately and multiple times you could use lables and the restore command


restore mynames
for x=1 to 4
read name$
print name$
next x

restore mynums
for x=1 to 4
read num
print num
next x

mynums:
data 0,1,5,7,9

mynames:
data "John","Sam","Sue,"Jean"



Hope that helps!

Just in case you weren't sure what i meant about disregarding 0. In DB/DBC arrays start at 0 and go up to the number in the dim statement

so

dim myarray(2,2) does not contain 4 elements; but rather nine. like this

myarray(0,0)
myarray(0,1)
myarray(0,2)
myarray(1,0)

myarray(1,1)
myarray(1,2)
myarray(2,0)
myarray(2,1)
myarray(2,2)



~zen


NanoBrain
19
Years of Service
User Offline
Joined: 20th Jan 2005
Location: Portland, OR
Posted: 28th May 2005 13:20
Guitarman,

Data statements are a bit tricky to first use. However, if you read the help files, and be well observant, their gears are easy to turn. Just remember, that whatever type of data you are reading(string,real number or integer value), make sure that the variable the value is being read into, is of the same type. If data is a string, then read it into a string variable. Study my simple example below.




+NanoBrain+

Login to post a reply

Server time is: 2024-09-23 21:23:53
Your offset time is: 2024-09-23 21:23:53