in reply to Difference Of Two Strings

my $str="abcd"; my $letters = "ab"; print not_in($str, $letters),"\n"; sub not_in { my $str = shift; my $letters = shift; my (%hash1, %hash2); my @arr1 = split //, $str; @hash1{@arr1} = @arr1; my @arr2 = split //, $letters; my @deleted = grep $_, delete @hash1{@arr2}; @hash2{@arr2} = undef; delete @hash2{@deleted}; return if %hash2; join '', keys %hash1; }
Updated to more accurately reflect the requirements :-)
Actually, this one doesn't remove characters one for one as it seems you want to. If a character appears in the second arg, it removes all of that character in the first arg. That's not what you wanted is it??

Replies are listed 'Best First'.
Re: Re: Difference Of Two Strings
by YuckFoo (Abbot) on Nov 03, 2001 at 05:13 UTC
    Thanks runrig, I didn't even try the hash method. I thought it would be too slow. I was surprised how quick it was.

    l_runrig: 17 wallclock secs (17.23 usr + 0.00 sys = 17.23 CPU) @ 5943.12/s (n=102400)
    leftover: 16 wallclock secs (15.86 usr + 0.00 sys = 15.86 CPU) @ 6456.49/s (n=102400)