I am optimizing (for speed) my anagram script. I want to optimize this 'leftover' subroutine. It returns a string with characters that are in the first string that are not matched in the second string. Both strings are sorted. If the second string contains characters that can't be matched in the first string, undef is returned.
acciimmnnnnoootu - acimnotu = cimnnnoo
acciimmnnnnoootu - acimnotuu = undef
Spliting both strings to arrays first seems a bit brutish and expensive, but I can't find a faster way. Any ideas?
Thanks!
#!/usr/bin/perl use strict; my ($full, $part) = @ARGV; my ($left) = leftover($full, $part); if (defined($left)) { print "$full - $part = $left\n"; } else { print "$full - $part = undef\n"; } #----------------------------------------------------------- sub leftover { my ($full, $part) = @_; my ($ch, $left); my ($regx) = join('+.*', (split('', $part))); if ($full =~ m{$regx}) { my (@fulls) = split('', $full); my (@parts) = split('', $part); while ($ch = shift(@fulls)) { ($ch eq $parts[0]) ? shift(@parts) : ($left .= $ch); } } else { $left = undef; }; return $left; }
In reply to Difference Of Two Strings by YuckFoo
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |