X-Git-Url: https://git.donarmstrong.com/?a=blobdiff_plain;f=db-hash;fp=db-hash;h=d9ad8af30d0746c9b89e9e35da16ef65ac5efcfb;hb=3300ac8c577fe9138b330f7b8f596963085852ac;hp=db7066e6110fc4796b7b4d4a4c2417ee36e4f400;hpb=ae559de8acc0f097eda09a238a88892f5e1e8e43;p=bin.git diff --git a/db-hash b/db-hash index db7066e..d9ad8af 100755 --- a/db-hash +++ b/db-hash @@ -57,6 +57,11 @@ Output the key as well as the value. (Off by default) 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 +94,14 @@ 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!', 'missing_newline|missing-newline|n!', 'debug|d+','help|h|?','man|m'); @@ -130,23 +137,19 @@ if (not defined $options{include_key}) { } -if ($options{update}) { +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)]; - } - for my $key (keys %fast_db) { - $t_db{$key} = $fast_db{$key}; - } -} -elsif ($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) { + $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 +157,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 {