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 / Need help understanding Loading data from text files into arrays/UDT. DBP

Author
Message
Nul error
19
Years of Service
User Offline
Joined: 12th Aug 2005
Location:
Posted: 9th Aug 2006 20:40 Edited at: 9th Aug 2006 20:43
Maybe Im alittle sleepy but I need help understanding how to load data from a text file to a array/UDT. I sorta got the understanding of string parsing so I will skip that part.

lets say in the text file is
name1,5,0.44330330303
name2,8,-2.84534

k after the types are labled and parsed would you load the data into the array like this?


so it be the same as doing the data file from inside like this



or you have to do it another way?
Sorry this is confusing. like I said eariler im bit sleepy and my brain cells decided to go on strike.
Mr X
18
Years of Service
User Offline
Joined: 25th Sep 2005
Location: Universe, milkyway, sol-system, Earth...
Posted: 10th Aug 2006 02:52 Edited at: 10th Aug 2006 02:53
Well, as a start you shouldnt dim the array inside the FOR-NEXT loop. I even think that would erase the previus data stored in it. And then there is the problem with reading it.

Personally I would do something like this (sorry, the code taggs does not work on this computer, and if the code seem a bit weard, its because Ive not been at home for weeks, so its a long time since I used dbpro and this computer is missing sone symboles):

if file exist("afile.txt")=0
open to write 1,"afile.txt"
for n=1 to 10
write string str(rnd(100))
next n
close file 1
print "Press any key to exit"
else
dim stuff(10)
open to read 1,"afile.txt"
for n=1 to 10
read string stuff(n)
next n
close file 1
print "Press any key to view file contence."
wait key
cls
for n=1 to 10
print str(n),": ",stuff(n)
next n
print "Press any key to exit
endif
wait key
end

I hope it helps and that there is not too many problems with it.
Nul error
19
Years of Service
User Offline
Joined: 12th Aug 2005
Location:
Posted: 10th Aug 2006 05:25
yah I was dead tired when I placed the for above the dim.
I was wondering if using this would be ok to load the data into the UDT or would there be a better way to do this.
lower logic
18
Years of Service
User Offline
Joined: 15th Jun 2006
Location:
Posted: 10th Aug 2006 07:07 Edited at: 10th Aug 2006 07:09


This would move the literal string "var1" into stuff(x).var1, the integer var2 into stuff(x).var2 (which is what we want), and the float, truncated(cutting off everything after the decimal) to an integer, var3, into stuff(x).var3 .
To make sure this is clear, typing
"Type UDTtest
var1 as string
var2 as integer
var3 as float
endtype"
will not make the actual variable var1 a string, etc. Just when it is used in an array, so stuff(x).var1 will be a string.

By default, all variables with no signs after them are integers, so var1, var2, and var3 would all be integers.

If you want var1 to hold the contents of a string, you would need to do something like "var1 as string" outside the type structure. You would also need to do "var3 as float" outside the type structure to specify that var3 is a float. Alternatively, you could use var1$, var2, and var3#. This is because the $ sign makes the variable a string by default and the # sign makes the variable a float by default. Then all you would need to do is load the string into var1$, integer into var2, and float into var3#, and then do:

Also, the $ and # signs make it easy to see what the type of variables are, as all variables that end in $ are strings, etc.
Nul error
19
Years of Service
User Offline
Joined: 12th Aug 2005
Location:
Posted: 10th Aug 2006 14:30
Think Im getting it. the hamster slowly turning the wheel.
Mr X
18
Years of Service
User Offline
Joined: 25th Sep 2005
Location: Universe, milkyway, sol-system, Earth...
Posted: 12th Aug 2006 00:29
Ok, now Im at home and have corrected my code.

Login to post a reply

Server time is: 2024-09-25 05:24:17
Your offset time is: 2024-09-25 05:24:17