aboutsummaryrefslogtreecommitdiff
path: root/challenge-004/james-smith/perl5/ch-2.pl
blob: 216374e9d72d92da33eff4ba320f1a9953859b9d (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
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;

for my $word (<STDIN>) { chomp $word;
  say $word if checkword(lc$word,%counts);
}

## 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($w,%c)=@_;
  for(split//,$w) {return if --$c{$_}<0}
  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;
## }