If your given a letter can it be matched more than once if it's only given once, or can it only be matched as many times as it's given?
ie a,e,h,r,t would match 'hater', but would it match 'hatter' or would it need to be a,e,h,r,t,t to match 'hatter'?
If the first is true
pbeckingham's code will work, if not this may do what your looking for.
use strict;
use warnings;
my %words = map { $_ => countLetters($_)} qw/tar rat at attitude tap o
+ther/;
my $input = 'art';
my $input_letters = countLetters($input);
my @matched_words;
WORD: for my $word (sort keys %words) {
for my $letter (keys %{$words{$word}}) {
next WORD unless exists($input_letters->{$letter}) && $input_l
+etters->{$letter} >= $words{$word}{$letter};
}
push @matched_words, $word;
}
print "Matched words: @matched_words\n";
sub countLetters {
my $word = shift;
my %letters;
$letters{$_}++ for split //, $word;
return \%letters;
}
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.