This is the new home of the egghelp.org community forum.
All data has been migrated (including user logins/passwords) to a new phpBB version.


For more information, see this announcement post. Click the X in the top right-corner of this box to dismiss this message.

i need some help with database

Old posts that have not been replied to for several years.
Locked
D
Drewbu

Post by Drewbu »

What's the best way i can make db's in files .... and how i can get vars from it very easy ?
I was thinking have file
var => 1
var2 => 2
in perl i could use split comand to get rid of => ....
can anyone help me?
User avatar
stdragon
Owner
Posts: 959
Joined: Sun Sep 23, 2001 8:00 pm
Contact:

Post by stdragon »

Probably the easiest way is to make the file like this:

varname1
data1
varname2
data2
varname3
data3
...

To read it:
set fp [open data.txt r]
set pairs [split [read $fp] n]
close $fp
array set data $pairs

Now you can access the data by saying $data(varname)

To write the data to the file:
set pairs [array get data]
set fp [open data.txt w]
puts -nonewline $fp [join $pairs n]
close $fp

Just make sure your data doesn't have linebreaks in it.
P
Petersen
Owner
Posts: 685
Joined: Thu Sep 27, 2001 8:00 pm
Location: Blackpool, UK

Post by Petersen »

actually the easier way would to be to make the file in direct tcl source.

ie

set var1 "blah"
set var2 "bleh"

then just read it in with source
you may wanna enclose the source command from inside a catch command though incase the datafile is corrupt.
User avatar
stdragon
Owner
Posts: 959
Joined: Sun Sep 23, 2001 8:00 pm
Contact:

Post by stdragon »

Assuming the data is dynamic and needs to be written to the file on occasion, that way is a lot harder. You either have to dedicate a namespace to it, or have a list of all the variable names that you want to write out. Also, you have to be careful of the content. Instead of only being sensitive to newlines, now you have to watch for unbalanced quotes/braces and of course evaluation brackets.
D
Drewbu

Post by Drewbu »

ok thanks i will think about it ...
Locked