in reply to Weirdness when reading from DBM file

Warning: Completely and utterly untested.
use DBM::Deep; my $dbh = get_dbh(); # This is the magic. :-) my $db = DBM::Deep->new( 'db_file.db' ); my @keys = qw(employee_id firstname lastname title dept salary date_hired); my @values = @{$dbh->selectall_arrayref($query)}; for my $v (@values){ my @vals = @$v; # one ID per array for (@vals){ $_ = '-' unless $_; # in case of empty fields } # Map is a wonderful piece of magic. Look really really hard at # this code before freaking out. $db->{$vals[0]} = { (map { $keys[$_] => $vals[$_]; } 0 .. $#keys) }; }

My criteria for good software:
  1. Does it work?
  2. Can someone else come in, make a change, and be reasonably certain no bugs were introduced?

Replies are listed 'Best First'.
Re^2: Weirdness when reading from DBM file
by mojodaddy (Pilgrim) on Sep 12, 2007 at 01:08 UTC
    Just wanted to report back on my results, which were phenomenal! This script writes the MySQL database to the DBM file:
    #!/usr/bin/perl -w use strict; use Mojo::DB; use DBM::Deep; $| = 1; my $dbh = get_dbh(); die "No filename argument provided" unless @ARGV; my $file = shift @ARGV; unlink $file; # This is the magic. :-) my $db = DBM::Deep->new( $file ); tie my %IDS, 'DBM::Deep', $file or die "$!"; my @keys = qw(employee_id firstname lastname title dept salary date_hi +red); my $keys = join(', ', @keys); my $query = "select $keys from employee_data"; my @values = @{$dbh->selectall_arrayref($query)}; for my $v (@values){ my @vals = @$v; # one ID per array for (@vals){ $_ = '-' unless $_; } $db->{$vals[0]} = { (map { $keys[$_] => $vals[$_]; } 0 .. $#keys) }; }
    and this script reads the data back out:
    #!/usr/bin/perl -w use strict; use DBM::Deep; $| = 1; die "No filename argument provided" unless @ARGV; my $file = shift @ARGV; tie my %IDS, 'DBM::Deep', $file or die "$!"; while (my($k, $v) = each %IDS){ print "\n$k :\n"; while (my ($k, $v) = each %{$v}){ print "$k => $v\n"; } }
    It really works like a dream, thank you so much. The nice thing, too, is that I almost understand how it works! I'm even going to leave your comment about "this is the magic" in there to remind me. : )


    Any technology sufficiently advanced is indistinguishable from a Perl script.
      *smiles* I'm glad it worked out for you. If you run into any problems with DBM::Deep, please let me know.

      As for map, it's conceptually pretty simple. You're used to functions taking a single thing as an argument and returning a single thing as a result. Map takes an action and a list of things as arguments, does the action to each of the things in turn, and returns the list of actioned things as a result.

      In the code above, what I'm doing is taking a list of indices and inflating each one into a key-value pair. Then, I'm using the fact that a hash can be assigned a list of key-value pairs to create my hash. Does that help make more sense?


      My criteria for good software:
      1. Does it work?
      2. Can someone else come in, make a change, and be reasonably certain no bugs were introduced?
Re^2: Weirdness when reading from DBM file
by mojodaddy (Pilgrim) on Sep 12, 2007 at 00:24 UTC
    Very nice, thank you for that. I really do think of map as a kind of magic. It reminds me of this quote:

    "Any technology sufficiently advanced is indistinguishable from a Perl script."

    Hey, I think I just found my clever signature line. But if someone else is already using it, let me know...