... 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.

perl -le'print map{pack c,($-++?1:13)+ord}split//,ESEL'

In reply to Re: The Art of Hashing by shmem
in thread The Art of Hashing by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.