It's easy to sort the words into sets if it's you're just matching the first 3 characters. See above. But what if the criteria are more complex? What if the words are considered a match if a block of letters inside the words comprising at least x% of the total letters matches? You can't use keys and hashes for that. Does every word in a set have to match every other word in the set, or can the words be linked by "joiner" words that may match two or more words that don't match each other? Both situations create interesting algorithmic problems.
Probably the best thing to do is separate your match criteria from your set criteria by having match be a sub:
my $match = sub { return substr($_[0], 0, 3) eq substr($_[1], 0, 3); }
+;
print $match->('aaaa','aaab');
And then passing it to your set sub:
myset($match, @words);
sub myset {
my $match = shift;
# Do whatever set algorithm here,
# using $match to compare words
}
This allows you to easily test multiple match criteria without messy duplication of code. I'd have included set code too, but you weren't very specific as to what you really want in terms of match / set criteria. First three letters won't get you much of anywhere.
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.