Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:
Dear Brothers,
I would like to take arbitrary text input and manipulate it a bit. The input will be in a single scalar: $string. It might be null, empty, multiline or very large. I want to break everything into "words"; trim any leading, trailing or embedded white space; and get rid of line endings.
I have tried the following but the "map" is not doing what i expected. Is there an existing module that will do this better? What am I missing in this routine? Am I on the right track?
sub wordBreak { my ($string) = @_; my $tmp = ""; my @result = (); my @array = (); # # break the input into "lines" # @array = split(/^/m, $string); # # turn on swallow ending white space mode and # chomp the array elements # $tmp = $/; $/ = ""; chomp(@array) if ($#array >= 0); $/ = $tmp; @result = split( ' ', # split the one line into words join( ' ', # join the "lines" into one map { s/\s+/ /g; # get rid of extra whitespace s/^ //; s/ $//; } @array ) ) if ($#array >= 0); return @result; }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Smoothing text input
by Kenosis (Priest) on Aug 29, 2012 at 16:30 UTC | |
by Anonymous Monk on Aug 29, 2012 at 17:42 UTC | |
by Kenosis (Priest) on Aug 29, 2012 at 18:14 UTC | |
|
Re: Smoothing text input
by johngg (Canon) on Aug 29, 2012 at 17:08 UTC | |
by Anonymous Monk on Aug 29, 2012 at 17:43 UTC | |
|
Re: Smoothing text input
by choroba (Cardinal) on Aug 29, 2012 at 15:58 UTC | |
|
Re: Smoothing text input
by hbm (Hermit) on Aug 29, 2012 at 19:08 UTC |