mr_evans2u has asked for the wisdom of the Perl Monks concerning the following question:

I am needing to take out the third word of a sting. After a submit button is clicked I need to put that word back into the string. Does anyone have a good way of doing this?

Replies are listed 'Best First'.
Re: Needing to take out 3rd word of sting
by davido (Cardinal) on Sep 24, 2003 at 19:52 UTC
    There is, of course, more than one way to do it, and those ways are highly dependant on what you consider to be a word. If you are content with the \w description of word, you can use the following example. You will have to modify that example if you have a more specific description of word (such as needing words to contain hyphens, apostraphes, accent characters, etc.).

    You can remove the {n}th+1 word like this:

    my $string = "one two three four"; $string =~ s/^((?:\w+\s+){2})(\w+)\s+\b/$1/; my $word = $2; print "$string, $word\n";

    That should be enough ammunition for you to see how to put it back as well, but if not:

    $string =~ s/((?:\w+\s+){2})/$1$word /;

    This method has some shortcomings. One of them is that if your words are not space-delimited, you will end up with trouble. Also if your phrase of words contains punctuation, such as a comma or a period, that will also wreck havoc. But you didn't specify such details in the description of your problem. Ask the question filled with ambiguity and vagueness, you get an answer that may not solve your problem. Note that I put the space following the word back into the string. It's somewhat hard to see, but it's right after $word and before /;

    A few days ago you started a similar thread, take out section of a string then put it back. You received several good answers. In this response you were advised to read the perlre perldoc page. Had you done this, you would have already known the answer to today's question. To that reading assignment I would also add perlretut and perlrequick. In fact, reverse that order, read perlrequick, perlretut, and perlre in that order. At some point through that reading the regular expression light-bulb will flick on in your mind.

    I hope this advice helps ease the pain of diving into something new, and admittedly, somewhat overwhelming at first.

    Dave

    "If I had my life to do over again, I'd be a plumber." -- Albert Einstein

      I hope it flicks on soon, I'm still very new to all of the regular expressions coding and it is driving me nuts. By the way I did get the other question to work, had to mess with it some but did get it. Thanks
      Here is an example of what I'm considering a word:
      /\b(?:impotence(?:problem|cure|solution)|Premature Ejaculation|erectile dysfunction)/i
      It has various characters and I guess it does have whitespace. So it would need to be the third and any other after that
        Sorry didn't log in. Here is an example of what I'm considering a word:
        /\b(?:impotence(?:problem|cure|solution)|Premature Ejaculation|erectile dysfunction)/i
        It has various characters and I guess it does have whitespace. So it would need to be the third and any other after that
Re: Needing to take out 3rd word of sting
by Abigail-II (Bishop) on Sep 24, 2003 at 19:34 UTC
    First of all, what do you consider a word? Is "isn't" a word, or is it two? What about words with hyphens?

    Second, what exactly do you mean by "putting it back"? If you first take it out, then put it back, you have back the original thing, don't you? Or do you put it back elsewhere?

    Third, what submit button are we talking about? Perl doesn't have any submit button, so, what do you mean?

    Having said that, assuming you consider a word to be a sequence of \w chars, and any maximized sequence to be a word, this will remove the third word from a string:

    $str =~ /(\w+\W+\w+\W+)\w+/$1/

    Abigail

      Dont mean to be picky, fantastic solution, just that you forgot the 's' for substitution:

      $str =~ s/(\w+\W+\w+\W+)\w+/$1/;
      Sam
Re: Needing to take out 3rd word of sting
by seaver (Pilgrim) on Sep 24, 2003 at 19:30 UTC
    You would need to split the string according to what surrounds a word, im assuming in this case it is a space:
    @words = split ' ', $string;
    The third word would be $words[2] so you'd create the string again without that word:
    for($i=0;$i<scalar(@words);$i++){ $string .= $words[$i] unless $i==2; }
    Keep the array of words, and when you need to put it back in simply do:
    for($i=0;$i<scalar(@words);$i++){ $string .= $words[$i]; }
    Hope this helps

    Cheers
    Sam

      Applying that on the sentence:
      I - king of the world - rule.
      removes the word king. Most people will argue that the third word in the sentence is of.

      Abigail

      Of course, I forget, You need to put the space back in:
      $string .= $words[$i]." "
Re: Needing to take out 3rd word of sting
by Rich36 (Chaplain) on Sep 24, 2003 at 19:58 UTC

    A different way of approaching the problem - use a hash to store the order of the words and the words themselves. This makes it easy to delete or add words in the order that you want. This makes use of a property of delete() that I didn't realize existed - it returns the value of the hash element that it deletes.

    my $string = "This is a only a test"; my %words; my $i = 1; # Assign the order of the words as a key, the word itself as # a value $words{$i++} = $_ foreach(split(' ', $string)); # Delete the word that you want to delete my $deleted_word = delete($words{3}); # Put the word back in $words{3} = $deleted_word; # Join the values back together based on the order of # the keys $string = join(" ", map { $_ = $words{$_} } sort keys %words); print $string . "\n";

    «Rich36»