in reply to Way to "trim" part of a phrase?
Apart from that, if any of the subsequent lines were to be reached and executed, it seems like they're doing a lot of unnecessary work.sub cleanup_start_words { my $text = $_[0]; my @split = split //, $text; my @keywords = split //, $_[1]; return $text; # nothing from here down ever gets executed.
Maybe I'm misunderstaning what you're really trying to do, but if the goal is to eliminate the initial portion of a string up to and including the first occurrence of any of these five characters: [!,.:)] -- then a quick/easy way would be:
That will return the original phrase if it contains none of the targeted characters. When any of those characters is present, it will return the string that starts with the very next character (usually a space).sub delete_initial_phrase { my ( $phrase ) = @_; if ( $phrase =~ /([!,.:)])/ ) { my $punc = $1; return substr( $phrase, 1 + index( $phrase, $punc )); } else { return $phrase; } }
On thinking a bit more about the process, you probably want something slightly different, in case you run into examples like this:
Do you want the return value to be ") Foo bar, and so on...", or would you rather remove the initial paren along with the exclamation? If the latter, just change the initial regex:blah blah!) Foo bar, and so on...
if ( $phrase =~ /([!,.:)]+)/ ) {
return substr( $phrase, length($punc) + index( $phrase, $punc +));
(updated to fix a couple typos -- and to add the point about modifying the substr call)
|
|---|