#! /usr/bin/perl
-# , and is released
+# db-hash creates databases and queries it, and is released
# under the terms of the GPL version 2, or any later version, at your
# option. See the file README and COPYING for more information.
-# Copyright 2009 by Don Armstrong <don@donarmstrong.com>.
-# $Id: perl_script 1432 2009-04-21 02:42:41Z don $
+# Copyright 2009-11 by Don Armstrong <don@donarmstrong.com>.
use warnings;
--dump, -D dump dbname
--missing-newline, -n output a newline for unhashed keys
--include-key, -k output the key as well as the value
+ --reverse, -r reverse (values to key)
+ --key-to-itself map key to itself
--debug, -d debugging level (Default 0)
--help, -h display this help
--man, -m display manual
Output the key as well as the value. (Off by default)
+=item B<--reverse>
+
+Map values to a key
+
+=item B<--key-to-itself>
+
+Map the key to itself (On by default in reverse hashes)
+
=item B<--missing-newline,-n>
If a key doesn't exist in the database, output a newline. (Forced on
'dump|D',
'include_key|include-key|k!',
'reverse|r!',
+ 'key_to_itself|key-to-itself|K',
'missing_newline|missing-newline|n!',
'debug|d+','help|h|?','man|m');
die "Unable to open $db for writing: $!";
}
+if ($options{reverse}) {
+ if (not exists $options{key_to_itself}) {
+ $options{key_to_itself} = 1;
+ }
+}
+
if (not defined $options{include_key}) {
$options{include_key} = $options{dump} ? 0 : 1;
}
chomp;
my ($key,@val) = split /\t/;
if ($options{reverse}) {
- for my $val_key (@val) {
- $fast_db{$val_key} = [make_list($fast_db{$val_key})//[],$key];
+ for my $val_key (@val,$options{key_to_itself}?$key:()) {
+ $fast_db{$val_key} = [make_list($fast_db{$val_key} // []),$key];
}
}
else {