in reply to Blazingly FAST
in thread Fast sublist generation
Your first code would return ('ab', 'abc'). The code I'm replying to will return ('a', 'ab').%hash = (a => 1, ab => 1, abc => 1, ac => 1); $key = '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:
There are a few optimizations you can do (I'd think of more, but I haven't had my first pot of coffee yet):my @list_of_keys = sort grep /(^$search|$search$)/o, keys %hash;
</code> Please note that this code is completely untested.my @list_of_keys = sort grep { substr($_, 0, length($search)) eq $sear +ch || substr($_, -length($search)) eq $search + }, keys %hash;
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Re: Blazingly FAST (and very right)
by PetaMem (Priest) on Jul 30, 2001 at 18:01 UTC | |
by dragonchild (Archbishop) on Jul 30, 2001 at 18:17 UTC |