Hello, I have a hash with international phone numbers which looks like this:
%hash = (31123 => 1, 31456 => 2, 32123 => 3, 32456 => 4, 33 => 5, 3361 => 6, ... );
The first two digits are the country code, the other three are the prefix of the subscribers number. This hash can contain more than 100 of these prefixes, where the prefixes are of variable length. Now I have a single phone number e.g. 31456000001. I would like to do ONE lookup in the hash to find that value corresponding with this prefix (31456, which is 2). But the problem is that the prefixes have a random length as you can see in the hash, so I cannot use the first 5 digits of the phonenumber as the key. It would be nice to specify a *-sign to use as a wildcard in the hash, e.g.:
%hash = (31123 => 1, 31456 => 2, 32123 => 3, 32456 => 4, 33* => 5, 3361* => 6, ... );
where %hash->{33000000000} would give me 5. Is there a way to accomplish this with only ONE lookup instead of using a regexp on all keys in the array? TIA! Regards, Marcel

In reply to Fast lookup for prefixes in hash by Marcello

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.