in reply to Is this a reasonable data structure?
The data file -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);
And the hash structure built with the above script: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
$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 | |
|
2Re: Is this a reasonable data structure?
by jeffa (Bishop) on Oct 24, 2003 at 15:15 UTC | |
|
Re: Re: Is this a reasonable data structure?
by ndwg (Beadle) on Oct 24, 2003 at 18:38 UTC |