Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game. |
Notices |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
|
07-13-2001, 02:35 PM
|
#1
|
Member
Registered: Jun 2001
Location: Virginia
Distribution: Slackware,Debian,SuSE
Posts: 43
Rep:
|
perl - writing a data structure to a file.. is it possible?
would it be possible to write a data structure, such as an anonymous hash of hashes, to a file for later use? say i am using a data type like this as a sort of database for my program and i don't always want to have it in memory or i want to save it for later use... whatever.
and if this IS possible, will it really be as efficient as it sounds? i have the feeling that in most situations (i.e. really searching for data in a file) pattern matching on a text file might be the most efficient way to handle this, but just for the sake of argument... pretend that the program needs LOTS of these little databases and you want to be able to automatically initialize them on demand, and then file them away somewhere when they are no longer needed.
this is more of a theoretical/hypothetical question... i don't really have the need for this at the moment, but it does sound useful...
thanks.
|
|
|
07-13-2001, 03:14 PM
|
#2
|
Senior Member
Registered: May 2001
Location: Bristol, UK
Distribution: Slackware, Fedora, RHES
Posts: 2,243
Rep:
|
oh! Perl ahoy :)
When ever I work with hash's and need to save them I bind them to a DBM file. That way any changes are reflected on the DBM so they're always saved. Effectively they'll be stored in the DBM file when you close the DBM and you can open/close them as necessary.
Does this sound like the kind thing you want? Its a lot faster than doing a linear search of a text file, the growth rate that you get with search times doesn't effect DBMs so they're quite efficient, and once its bound to the hash you just address your data in the usual way using $myHash{$someKey} = $fooBar;
I can dig out some example code if this sounds like your sorta thing.
HTH
Jamie...
|
|
|
07-13-2001, 03:26 PM
|
#3
|
Member
Registered: Jun 2001
Location: Virginia
Distribution: Slackware,Debian,SuSE
Posts: 43
Original Poster
Rep:
|
why yes...
that sounds JUST like what i am looking for...
so, yes, if you could show me just a little bit of outline code on how this is done, i would be grateful... i haven't heard of DBMs. most of my perl experience has just been through trial and error... can't really afford one of those nice $60 books just yet..
|
|
|
07-13-2001, 04:06 PM
|
#4
|
Senior Member
Registered: May 2001
Location: Bristol, UK
Distribution: Slackware, Fedora, RHES
Posts: 2,243
Rep:
|
If you don't mind reading from your screen then I'd really recommend the following http://www.oreilly.com/catalog/perlcdbs2/ I've got the first version which is great
Here's some example code, lets create a DBM and put some data in it
Quote:
#!/usr/local/bin/perl -w
#-w is your friend!! Always use it...
#create a hash and link it to a file, open it read/write
dbmopen(%test, "testdb", 0666) or die "Cant open testdb file\n";
$test{"aKey"} = "foo";
$test{"anotherkey"} = "bar";
$test{"moreKeys"} = "fooBars";
#close the hash so the data is definately saved
dbmclose(%test);
|
Running the above should create a file called testdb, here's what I eneded up with on my system:
Quote:
root@mnemosyne:~/perl# chmod 700 test1.pl
chmod 700 test1.pl
root@mnemosyne:~/perl# ./!$
./test1.pl
root@mnemosyne:~/perl# ls -l
total 16
-rwx------ 1 root root 339 Jul 13 21:46 test1.pl*
-rw-r--r-- 1 root root 20480 Jul 13 21:46 testdb
root@mnemosyne:~/perl# file testdb
testdb: Berkeley DB 2.X Hash/Little Endian (Version 5, Logical sequence number:
file - 0, offset - 0, Bucket Size 4096, Overflow Point 2, Last Freed 0, Max Buck
et 3, High Mask 0x3, Low Mask 0x1, Fill Factor 0, Number of Keys 0)
root@mnemosyne:~/perl#
|
No lets write another script to read the data back and write it to the screen (really useful eh!! )
Quote:
#!/usr/local/bin/perl -w
#relink the DBM, open it read only this time
dbmopen(%test, "testdb", 0444) or die "Cant open testdb file\n";
#use 'foreach' to loop through all of the available keys, as 'foreach'
#expects an array we use 'keys %test' to return an array of
#all the keys. Each time through the loop $key will hold the
#next available key, it'll keep going until it's exhaused the
#supply of keys
foreach $key ( keys %test ) {
print("The value held in \%test, keyed on $key was => " . $test{$key} . "\n");
}
#close the hash so the data is definately saved
dbmclose(%test);
|
My system gave me the following when I ran the above
Quote:
root@mnemosyne:~/perl# ./test2.pl
The value held in %test, keyed on aKey was => foo
The value held in %test, keyed on anotherkey was => bar
The value held in %test, keyed on moreKeys was => fooBars
root@mnemosyne:~/perl#
|
Does that all make sense?? Or do you need a little more of an example? Don't ya just love Perl?
Jamie...
|
|
|
07-14-2001, 12:04 AM
|
#5
|
Member
Registered: Jun 2001
Location: Virginia
Distribution: Slackware,Debian,SuSE
Posts: 43
Original Poster
Rep:
|
no, i can follow that... pretty damn simple, really. thanks! (..and yeah, perl is great!)
|
|
|
All times are GMT -5. The time now is 10:54 AM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|