This doesn't do what you want it to. Let's take a small example:

%hash = (a => 1, ab => 1, abc => 1, ac => 1); $key = 'ab';
Your first code would return ('ab', 'abc'). The code I'm replying to will return ('a', 'ab').

Let's examine the problem. You need to find all the keys that start or end with a given string. To do this, you need to get a list of the keys.

Now, once you have that list of keys, treat it as if it's just a list of strings with no duplicates. The shortest Perl code to find this would be:

my @list_of_keys = sort grep /(^$search|$search$)/o, keys %hash;
There are a few optimizations you can do (I'd think of more, but I haven't had my first pot of coffee yet): Putting both together yields:

my @list_of_keys = sort grep { substr($_, 0, length($search)) eq $sear +ch || substr($_, -length($search)) eq $search + }, keys %hash;
</code> Please note that this code is completely untested.

In reply to Re: Blazingly FAST (... yet oh so wrong) by dragonchild
in thread Fast sublist generation by PetaMem

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.