Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:
I'm relativly new to Perl, and have never worked with flat file databases before.
I was given a flat file, pipe delimited database & need to generate reports based on the data said database. I'm having difficulty in determining the most efficient (but right now I'd settle for in-effecient if it gets the job done) way to sort through and pull out two entries from the database using a couple of search keys. I'll then format the data into a HTML based report in the fasion chosen by my PHB.
Any clues or tips would be better than Tylenol for my headache. Thanks
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Flat file DB
by tachyon (Chancellor) on Aug 30, 2001 at 01:58 UTC | |
Here is is in a nutshell. This example reads from the DATA filehandle. By default this is the stuff after __DATA__ at the end of the program but if you open $file onto it it will be the contents of that file. To do this just uncomment the # open DATA, $file....line. If you do a Super Search for "flatfile pipe delimited database" you will find heaps of stuff on this. If you have other problems supply your problem code and some sample data. Maybe have a look at New Monks if you want to good rundown on the Monastery and Perl all on one page.
cheers tachyon s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print | [reply] [d/l] |
|
Re: Flat file DB
by jryan (Vicar) on Aug 30, 2001 at 01:36 UTC | |
I assume your database looks something like this?
The easiest way to do it would be to open the file, put the data into an array, split each array element on the pipe, and then go from there.
This is really an inefficient way to store data... I suggest you upgrade to a (my|postgre|donthurtme) database. Perl works really well with them, has a lot of support, and they are much faster | [reply] [d/l] [select] |
by Anonymous Monk on Aug 30, 2001 at 02:18 UTC | |
entry1_stuff1|entry1_stuff2|entry1_stuff3 entry2_stuff1|entry2_stuff2|entry2_stuff3 entry3_stuff1|entry3_stuff2|entry3_stuff3 and so on... | [reply] |
|
Re: Flat file DB
by cLive ;-) (Prior) on Aug 30, 2001 at 01:43 UTC | |
| [reply] | |
|
Re: Flat file DB
by rbi (Monk) on Aug 30, 2001 at 15:41 UTC | |
I once wrote this code to learn some few things (I'm rather new to Perl, too). Save it into an executable file (I named it "agenda"), and it should work as a flat DB reader and printer. It is intended as an address manager... Probably silly (grep and few other things can do the same), but you can maybe grab some simple idea for your DB needs. ciao Roberto a sample phone.txt file:
| [reply] [d/l] [select] |
by Hofmator (Curate) on Aug 30, 2001 at 18:09 UTC | |
Allow me to comment a little bit on your code, you are sometimes making your life harder than it has to be ;-) I will make the comments sequentially with the source code. There are more things, e.g. I don't think the hash of arrays is the best datastructure for this task, you are not using the main feature of a hash, the direct access to an element by a key. It has probably been some time since you wrote this script, your perl has improved in the meantime. So as a good exercise I'd suggest to rewrite the whole thing, rethinking your data structure and incorporating my advices. If you then post your script here you will get further suggestions for improvement. And at the end of the day you will have learnt a lot :-) -- Hofmator | [reply] [d/l] [select] |