in reply to Is this a reasonable data structure?

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' } };

Replies are listed 'Best First'.
Re: Re: Is this a reasonable data structure?
by BrowserUk (Patriarch) on Oct 24, 2003 at 01:00 UTC

    Or he could do it in a quarter of the time with half the code, without having to download and compile modules or read 1000 lines of documentation and learn SQL.

    #! perl -slw use strict; use Data::Dumper; my @fields = split'\|', <DATA>; chomp $fields[-1]; my %HoH = map{ chomp; my%h; @h{ @fields } = split'\|'; ( $h{ last } . '_' . substr( $h{ first }, 0, 1 ) => \%h ) } <DATA>; print Dumper \%HoH; __DATA__ title|first|last|room|phone|email Mrs|Linda|Caralo|201|148|she@borg.org Miss|Jean|Androno|317|167|j@alo.com

    prints

    P:\test>test2 P:\test>test2 $VAR1 = { 'Paterman_S' => { 'email' => 'steve@net.net', 'first' => 'Steve', 'last' => 'Paterman', 'title' => 'Mr', 'phone' => '100', 'room' => '101' }, 'Caralo_L' => { 'email' => 'she@borg.org', 'first' => 'Linda', 'last' => 'Caralo', 'title' => 'Mrs', 'phone' => '148', 'room' => '201' }, 'Androno_J' => { 'email' => 'j@alo.com', 'first' => 'Jean', 'last' => 'Androno', 'title' => 'Miss', 'phone' => '167', 'room' => '317' } };

    Examine what is said, not who speaks.
    "Efficiency is intelligent laziness." -David Dunham
    "Think for yourself!" - Abigail
    Hooray!

2Re: Is this a reasonable data structure?
by jeffa (Bishop) on Oct 24, 2003 at 15:15 UTC
    DBD::CSV++

    However, you are doing too much work:

    ... # load address book entries my $sth = $dbh->prepare('SELECT * FROM addressbook'); $sth->execute(); my %data = map {$_->{last} => $_} @{$sth->fetchall_arrayref({})}; print Dumper \%data;

    jeffa

    L-LL-L--L-LL-L--L-LL-L--
    -R--R-RR-R--R-RR-R--R-RR
    B--B--B--B--B--B--B--B--
    H---H---H---H---H---H---
    (the triplet paradiddle with high-hat)
    
Re: Re: Is this a reasonable data structure?
by ndwg (Beadle) on Oct 24, 2003 at 18:38 UTC
    Along the same lines, but a little easier if you don't know SQL, you can try using AnyData.

    -Nathan