in reply to The Art of Hashing
... but not sure how to make (hash?) to just type smith and get both emails.
Two points here: 1) partial match and 2) multiple values to one key
1. For perl hashes, the only way to get matching keys of a hash for a given string, is performing a pattern match on the entire set of keys.
my @matching_keys = grep /$customer/, keys %Customer;
There are other hash implementations close to the perl core which live in extensions. For instance, there is DB_File which interfaces to Berkeley DB and provides binary trees. This implementation has partial match built in.
2. A perl hash consists of key/value pairs. If you want to store more than one item in the value slot, you have to store the reference to a container - a reference to an anonymous array or hash, which you later dereference.
# $Customer{$customer} = $_; push @{$Customer{$customer}}, $_; # use value slot as anonymous ar +ray # later # print "Customer: $Customer{$customer}\n"; print join("\n", "Customer:", @{$Customer{$customer}}), "\n" +;
Again, the DB_File module is an alternative here. Its BTREE file type optionally allows a single key to be associated with an arbitrary number of values. File isn't necessarily a external file, since Berkeley DB allows the creation of in-memory databases.
See grep, push, join and DB_File.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: The Art of Hashing
by BrowserUk (Patriarch) on Jun 16, 2014 at 09:24 UTC | |
by shmem (Chancellor) on Jun 16, 2014 at 13:01 UTC | |
by CountZero (Bishop) on Jun 16, 2014 at 16:49 UTC |