LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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

Reply
 
Search this Thread
Old 07-13-2001, 02:35 PM   #1
paavaka
Member
 
Registered: Jun 2001
Location: Virginia
Distribution: Slackware,Debian,SuSE
Posts: 43

Rep: Reputation: 15
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.
 
Old 07-13-2001, 03:14 PM   #2
jharris
Senior Member
 
Registered: May 2001
Location: Bristol, UK
Distribution: Slackware, Fedora, RHES
Posts: 2,243

Rep: Reputation: 46
Talking 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...
 
Old 07-13-2001, 03:26 PM   #3
paavaka
Member
 
Registered: Jun 2001
Location: Virginia
Distribution: Slackware,Debian,SuSE
Posts: 43

Original Poster
Rep: Reputation: 15
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..
 
Old 07-13-2001, 04:06 PM   #4
jharris
Senior Member
 
Registered: May 2001
Location: Bristol, UK
Distribution: Slackware, Fedora, RHES
Posts: 2,243

Rep: Reputation: 46
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...
 
Old 07-14-2001, 12:04 AM   #5
paavaka
Member
 
Registered: Jun 2001
Location: Virginia
Distribution: Slackware,Debian,SuSE
Posts: 43

Original Poster
Rep: Reputation: 15
no, i can follow that... pretty damn simple, really. thanks! (..and yeah, perl is great!)
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
writing raw data to a tiff file James_dean Programming 4 10-25-2005 05:03 AM
Perl: Inserting new data into the middle of a file R00ts Programming 5 03-07-2005 06:48 PM
Wait Free Data Structure neo_119 Programming 0 11-27-2004 01:44 AM
problem with writing into a file using socket(perl) akaash Programming 3 04-08-2004 06:06 AM
perl(Cwd) perl(File::Basename) perl(File::Copy) perl(strict)....What are those? Baldorg Linux - Software 1 11-09-2003 08:09 PM


All times are GMT -5. The time now is 01:29 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration