X-Git-Url: https://git.donarmstrong.com/?a=blobdiff_plain;f=db-hash;h=9b159ff85d716e5d3ee484a37afc1f8e2bf34679;hb=dcce90e77c92202b5fadca48de6fd1cefc41ff79;hp=db7066e6110fc4796b7b4d4a4c2417ee36e4f400;hpb=978bf233a782c62f6ea33399b4effc98bcf2c6a6;p=bin.git diff --git a/db-hash b/db-hash index db7066e..9b159ff 100755 --- a/db-hash +++ b/db-hash @@ -1,9 +1,8 @@ #! /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 . -# $Id: perl_script 1432 2009-04-21 02:42:41Z don $ +# Copyright 2009-11 by Don Armstrong . use warnings; @@ -27,6 +26,8 @@ db-hash - create a database and query using it --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 @@ -52,11 +53,24 @@ Dump database to stdout 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 when --include-key set) +=item B<--reverse,-r> + +Reverse the database (value looks up keys instead); only useful during +creation/update + =item B<--debug, -d> Debug verbosity. (Default 0) @@ -89,12 +103,15 @@ my %options = (debug => 0, dump => 0, # include_key => 0, missing_newline => 1, + reverse => 0, ); GetOptions(\%options, 'create|c','update|u', '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'); @@ -125,28 +142,30 @@ else { 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; } -if ($options{update}) { - my %fast_db; - while () { - chomp; - my ($key,@val) = split /\t/; - $fast_db{$key} = [make_list($fast_db{$key} // [],@val)]; - } - for my $key (keys %fast_db) { - $t_db{$key} = $fast_db{$key}; - } -} -elsif ($options{create}) { +if ($options{update} or $options{create}) { my %fast_db; while () { chomp; my ($key,@val) = split /\t/; - $fast_db{$key} = [make_list($fast_db{$key} // [],@val)]; + if ($options{reverse}) { + for my $val_key (@val,$options{key_to_itself}?$key:()) { + $fast_db{$val_key} = [make_list($fast_db{$val_key} // []),$key]; + } + } + else { + $fast_db{$key} = [make_list($fast_db{$key} // [],@val)]; + } } for my $key (keys %fast_db) { $t_db{$key} = $fast_db{$key}; @@ -154,7 +173,7 @@ elsif ($options{create}) { } elsif ($options{dump}) { for my $key (keys %t_db) { - print "$key:".join("\t",make_list($t_db{$key}))."\n"; + print "$key\t".join("\t",make_list($t_db{$key}))."\n"; } } else {