princepawn has asked for the wisdom of the Perl Monks concerning the following question:
I want to "scrub" a second string of all characters which are not in the first string. Thus:
Carter's compass: I know I'm on the right track when by deleting something, I'm adding functionality
Here is the code I wrote. It works, but I was hoping there was a module or something which already did this.scrub 'pure', 'p!u-r-*e'; # returns pure
sub string_clean { my ($pure, $dirty) = @_; my @pure = (split //, $pure) ; my @dirty = (split //, $dirty) ; my %pure = map { ($_ => 1) } @pure; my @cleaned = grep { $pure{$_} } @dirty; return join '', @cleaned; }
slickness
staring at the sample code, I'm thinking how slick it would look to call the function like this:you know... use the arrow to show the transition from dirty to clean... real English-like.scrub 'impure' => 'pure' ; # yields 'pure'
Carter's compass: I know I'm on the right track when by deleting something, I'm adding functionality
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Scurbbing a string
by moritz (Cardinal) on Aug 24, 2007 at 21:03 UTC | |
by FunkyMonk (Chancellor) on Aug 24, 2007 at 21:09 UTC | |
by Roy Johnson (Monsignor) on Aug 24, 2007 at 21:31 UTC | |
Re: Scrubbing a string
by Limbic~Region (Chancellor) on Aug 24, 2007 at 22:31 UTC | |
Re: Scurbbing a string
by BrowserUk (Patriarch) on Aug 24, 2007 at 21:06 UTC | |
by Anonymous Monk on Aug 25, 2007 at 09:17 UTC | |
by BrowserUk (Patriarch) on Aug 25, 2007 at 09:57 UTC | |
by BrowserUk (Patriarch) on Aug 28, 2007 at 02:33 UTC | |
Re: Scurbbing a string
by FunkyMonk (Chancellor) on Aug 24, 2007 at 21:05 UTC |
Back to
Seekers of Perl Wisdom