in reply to Check if a word is included in another

sub is_included { my ($a,$b)=@_; # while ($a) { # UPDATED since buggy while (length $a) { my $c=substr($a,0,1); return 0 if $a =~ s/$c//g > $b =~ s/$c//g ; } return 1; } print is_included(qw/ababa babababab/); # 1 print is_included(qw/babababab ababa/); # 0 print is_included(qw/EASYPEASY PEARLYGATES/); # 0
HTH! :)

Cheers Rolf

Replies are listed 'Best First'.
Re^2: Check if a word is included in another
by b4swine (Pilgrim) on Apr 18, 2010 at 22:43 UTC
    I like it. Straightforward, easy to read, and probably more efficient than making hashes, etc. Need to speed test to make sure. This is most like what I was looking for.
      Thanx, I like my code too! ;-)

      Unfortunately there is a typical "too clever for perl" bug 8-(

      please replace:

      # while ($a ) { while (length $a ) {

      otherwise qw/0 a/ will be a false positive!

      If you want performance you may wanna try tr///d instead, but you will need to eval.

      Cheers Rolf