Seems that nobody has pointed this out yet - there is a convenient way to access data stored in your flat file. By using DBI and DBD::CSV modules.

use strict; use DBI; use DBD::CSV; use Data::Dumper; # Connect to CSV database my $dbh = DBI->connect("DBI:CSV:csv_sep_char=\|") or die "Cannot connect: " . $DBI::errstr; $dbh->{'csv_tables'}->{'addressbook'} = {'file'=>'addressbook.txt' }; # load address book entries my $sth = $dbh->prepare("SELECT * FROM addressbook"); $sth->execute(); # store data in 2-tier hash table my %data; while (my $res = $sth->fetchrow_hashref()) # loop through data { # create hash to store details my %rec = map { $_ => $res->{$_} } @{$sth->{NAME}}; # create top level hash with last name as lookup key $data{$rec{"last"}} = \%rec; } # cleaning up $sth->finish; $dbh->disconnect; # inspect our result print Dumper(\%data);
The data file -
addressbook.txt --------------- title|first|last|room|phone|email Mrs|Linda|Caralo|201|148|she@borg.org Miss|Jean|Androno|317|167|j@alo.com Mr|Steve|Paterman|101|100|steve@net.net
And the hash structure built with the above script:
$VAR1 = { 'Caralo' => { 'email' => 'she@borg.org', 'first' => 'Linda', 'last' => 'Caralo', 'title' => 'Mrs', 'phone' => '148', 'room' => '201' }, 'Paterman' => { 'email' => 'steve@net.net', 'first' => 'Steve', 'last' => 'Paterman', 'title' => 'Mr', 'phone' => '100', 'room' => '101' }, 'Androno' => { 'email' => 'j@alo.com', 'first' => 'Jean', 'last' => 'Androno', 'title' => 'Miss', 'phone' => '167', 'room' => '317' } };

In reply to Re: Is this a reasonable data structure? by Roger
in thread Is this a reasonable data structure? by Theo

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.