in reply to Difference Of Two Strings

I wanted to remove the split() entirely, but my knowledge of Perl isn't strong enought yet to come up with that solution. Here's a slight improvement that I came up with.

#!/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); my (@parts) = split('', $part); my ($regx) = join('+.*', @parts); if ($full =~ m{$regx}) { foreach $ch (@parts) { $full =~ s/[$ch]{1}//; } } else { $full = undef; }; return $full; }

Here's pseudo code for what my original idea was (maybe someone else can make it work):

sub leftover{ my ($full, $part) = @_; while ($full && $part) { #RexExp to substitute nothing for the first match #then remove the matched character from $part } }

Replies are listed 'Best First'.
Re: Re: Difference Of Two Strings
by YuckFoo (Abbot) on Nov 03, 2001 at 05:22 UTC
    Seems anything with s// in a loop doesn't stand a chance.
    I played with @- and @+ from s//, but these too, are expensive.
    I think this is what you are talking about in the pseudocode?

    l_jungleboy: 28 wallclock secs (28.84 usr + 0.00 sys = 28.84 CPU) @ 3550.62/s (n=102400)
    leftover: 16 wallclock secs (15.87 usr + 0.00 sys = 15.87 CPU) @ 6452.43/s (n=102400)