I'm not able to understand what you are trying to accomplish with the OP code. I'm especially puzzled by the first "return" statement, which is the fourth expression in the subroutine:
sub cleanup_start_words { my $text = $_[0]; my @split = split //, $text; my @keywords = split //, $_[1]; return $text; # nothing from here down ever gets executed.
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.

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:

sub delete_initial_phrase { my ( $phrase ) = @_; if ( $phrase =~ /([!,.:)])/ ) { my $punc = $1; return substr( $phrase, 1 + index( $phrase, $punc )); } else { return $phrase; } }
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).

On thinking a bit more about the process, you probably want something slightly different, in case you run into examples like this:

blah blah!) Foo bar, and so on...
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:
if ( $phrase =~ /([!,.:)]+)/ ) {
The rest stays the same. Then you also have to change the substr call:
return substr( $phrase, length($punc) + index( $phrase, $punc +));

(updated to fix a couple typos -- and to add the point about modifying the substr call)


In reply to Re: Way to "trim" part of a phrase? by graff
in thread Way to "trim" part of a phrase? by ultranerds

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.