Hi,
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
#!/usr/bin/perl use strict; my $ref_values; my $ref_names; ### Database file my $db_file = './phone.txt' || $ARGV[1]; ### Field eparator my $separator = '#' || $ARGV[2]; ### Set the primary key my $pri = 1 || $ARGV[3]; my $primary = $pri - 1; my $keyword = $ARGV[0] || die qq( \n), qq(use: agenda keyword (databasefile separator pos.key)\n), qq( \n), qq( default database: $db_file\n), qq( default separator: $separator\n), qq( default pos.key: $pri\n), qq( \n), qq( examples of use:\n), qq( agenda smith (shows all '*smith*' in field 'pos.key')\n), qq( agenda . (shows all records)\n), qq( \n), qq( To create a database, edit a text file writing in the firs +t\n), qq( line the names of the fields, separated by some *separator +*\n), qq( ('#' for example), and related entries in the following re +cords\n), qq( \n); ### Load the variables ($ref_values,$ref_names) = db_read($db_file, $separator, $primary); ### Print the database content db_print($ref_values,$ref_names,$keyword); ############ sub db_read{ ############ ### ### This routine reads the content of an ASCII database ### with field names as first record, storing the content ### into an hash of arrays. ### ### use strict; my $db_file = shift(); ### database file name my $separator = shift(); ### field separator my $primary = shift(); ### primary key my %values; my @fields; my @names; my $i; ### Open the database for reading open DB, "<$db_file" or die "Can't open $db_file: $!\n"; ### Read the first record containing the names of the fields my $first_record = <DB>; ### Remove the newline chomp $first_record; ### Split the record to get the field names @names = split($separator,$first_record); ### Now read the rest of the file and store ### the records into an hash of arrays while (<DB>) { ### Remove the new line character at end of record chomp; ### Split the record into fields @fields = split /$separator/; ### Populate the hashes defined by the fields in $first_record foreach $i (0..$#names-1) { $values{$fields[$primary]}[$i] = $fields[$i]; } } ### Close database close DB; ### Return the the reference to the hash of arrays ### and the array of field names return (\%values,\@names); } ############# sub db_print{ ############# ### ### This routine prints the content of an hash of arrays ### sorted by its keys. The hash of arrays is created by ### db_read subruotine. ### use strict; my %values = %{ shift() }; my @names = @{ shift() }; my $keyword = shift(); my $item; my $features; for $item ( sort keys %values ) { ### Scan the hash of arrays searching for $keyword my $found = 0; for $features (0..$#names-1) { if ($item =~ /$keyword/i or $values{$item}[$features]=~ /$keyword/i) { $found = 1; } } ### Print the hash of arrays if anything was found if ($found) { printf qq(\n ** keyword: %s\n\n), $item; for $features (0..$#names-1) { printf qq( %s = %s\n), $names[$features], $values{$item}[$features]; } $found = -1; } } }
a sample phone.txt file:
1#COGNOME#NOME#ENTE#UFFICIO#FAX#CELLULARE#CASA 2#Smith#John#CNR ICG#012 306816#012 304056## 3#Doe#John#AAA Corp.#012 306816#012 304056#335 2321212#

In reply to Re: Flat file DB by rbi
in thread Flat file DB by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.