blob: fd28d62886b59bfef1017b5f1dcec3d83c1b5377 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
|
use strict;
use warnings;
use feature qw(say);
## Read in letters from command line... and store in %c...
## We split each argument so words can be passed in rather
## than individual letters if required...
my %counts; $counts{lc$_}++ for map{split//} @ARGV;
say $_ for grep { chomp $_; checkword(lc$_) } <STDIN>;
## Check the word to see if it can be made up from letters
## use passing a hash by value to clone the counts so we
## don't destroy it through each loop {the method is
## destructive!}
sub checkword {
my %t;
foreach(split//,shift){
return 0 unless $counts{$_} && $t{$_}++ < $counts{$_};
}
return 1;
}
## Below is the first try - not as elegant - but no function
## calls - just needs to use labels to jump out of inner for
## loop
##
## my $c;
## $c->{lc $_}++ foreach @ARGV;
## WORD: while (my $w=<STDIN>) {
## chomp $w;
## my %t = %{$c};
## for (split //,lc $w) {
## next WORD if --$t{$_} < 0;
## }
## say $w;
## }
|