in reply to Difference Of Two Strings
You'll have to be careful in your usage of this sub, since your specs state nearly opposite meanings for the return values of '' and undef.#!/usr/bin/perl -wT use strict; no warnings "uninitialized"; # printing undef triggers a stupid warni +ng # testing loop for my $pair (['aabbcc', 'abc'], ['aabbccdd', 'abcdd'], ['abc', 'abc'], ['abc', '123'], ) { my $left = leftover(@$pair); printf ("%8s - %-6s => %-5s\n",@$pair,"'$left'"); } # scrabble "subtraction" subroutine sub leftover { my $string = shift; my $letters = shift; $string =~ s/$_// || return for split // => $letters; return $string; } =OUTPUT aabbcc - abc => 'abc' aabbccdd - abcdd => 'abc' abc - abc => '' abc - 123 => ''
Update: added || return to test for unmatched chars.
-Blake
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Re: Difference Of Two Strings
by YuckFoo (Abbot) on Nov 03, 2001 at 05:05 UTC | |
by blakem (Monsignor) on Nov 03, 2001 at 05:07 UTC |