in reply to Check if a word is included in another

$ perl -le' my $target = "PEARLYGATES"; my $word = "EASYPEASY"; ( my $temp = $target ) =~ s/[^$word]//g; print join( "", sort split //, $temp ) eq join( "", sort split //, $wo +rd ) ? "Match" : "No match"; ' No match

Replies are listed 'Best First'.
Re^2: Check if a word is included in another
by almut (Canon) on Apr 18, 2010 at 17:08 UTC

    Problem with this approach is that it doesn't take the numbers of each letter into account.  For example, EASY can be made from PEARLYGATES, yet it prints "No match", because it's checking AAEESY eq AESY.

Re^2: Check if a word is included in another
by PeterPeiGuo (Hermit) on Apr 18, 2010 at 18:06 UTC

    Interesting thought, just need a a little bit tweak:

    use warnings; use strict; print check("YLPP", "PEARLYGATES"), "\n"; print check("YLP", "PEARLYGATES"), "\n"; print check("EASYPEASY", "PEARLYGATES"), "\n"; sub check { my ($word, $target) = @_; foreach my $letter (split //, $word) { return "not ok" if ($target =~ s/$letter// == 0); } return "ok"; }

    Peter (Guo) Pei

      Isn't this the same as the original algorithm in the first post?

        Not the same, with or without /g makes a big difference in this case.

        use warnings; use strict; my $str1 = "112345"; $str1 =~ s/1//g; print $str1, "\n"; my $str2 = "112345"; $str2 =~ s/1//; print $str2, "\n";

        Peter (Guo) Pei