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

Hi I feel I am missing something obvious but I cannot qork out how to strip characters from a string and also return the characters stripped into another string. I use  $salutation   =~ s/\(.*?\)//;; and I would like to put the stripped stuff into another string $stripped. HOw can I achieve this please anyone?

Replies are listed 'Best First'.
Re: return stripped characters
by borisz (Canon) on Apr 26, 2005 at 15:46 UTC
    Or more verbose:
    $salutation =~ s/(\(.*?\))// and $stripped = $1;
    Boris
Re: return stripped characters
by inman (Curate) on Apr 26, 2005 at 16:18 UTC
    Declaration and conditional assignment.

    my $stripped = $salutation =~ s/(\(.*?\))// ? $1 : '';
Re: return stripped characters
by cog (Parson) on Apr 26, 2005 at 15:38 UTC
    For instance:

    $salutation =~ s/(\(.*?\))/$stripped .= $1;''/e;

    Don't forget to predeclare $stripped.

      I would recommend avoiding the eval by assigning as a separate step:
      $salutation =~ s/(\(.*?\))// and $stripped = $1;
      or some such.

      Caution: Contents may have been coded under pressure.
        True. My mind was thinking of global substitutions.

        Or, to use a conditional if construct:

        my $stripped; $stripped = $1 if $salutation =~ s/(\(.*?\))//;

        This is functionally equivalent to the parent post, just a different style of expressing it.

        Update: I was trying to be too concise, thanks to tlm for pointing me to the error of my ways. Code has been updated to predeclare $stripped, since that is much more reliable than declaration and assignment in one step with a conditional.

        radiantmatrix
        require General::Disclaimer;
        s//2fde04abe76c036c9074586c1/; while(m/(.)/g){print substr(' ,JPacehklnorstu',hex($1),1)}

      Hi sorry but I cannot get this to work
      $stripped=""; $salutation =~ s/(\(.*?\))/$stripped .= $1;''/e; print $stripped;
      $salutation is stripped as usual but $stripped is empty!
        It does work for me... What are you putting in $salutation?

        By the way, when I said "predeclare", I meant with my or our. I assume you're not using strict. You should.

Re: return stripped characters
by splinky (Hermit) on Apr 26, 2005 at 17:59 UTC
    I get the feeling we're all misunderstanding what you're trying to do. Just to clarify... are you trying to strip off everything in $salutation between two parentheses? In other words, if $salutation is "Dear Mr. Smith (you ignorant twit)", then you want to strip off "(you ignorant twit)", so that $salutation contains "Dear Mr. Smith ", and "(you ignorant twit)" is saved in some other variable, right?

    I ask this because that's certainly the question everyone else is answering for you.

Re: return stripped characters
by TedPride (Priest) on Apr 26, 2005 at 20:07 UTC
    Edit: Ignore post. Possibly misread requirements. Do you want the text inside the parentheses returned or the entire text?
      Hi everyone thanks for all your help all these solutions work perfectly for what I wanted. I have to say having a support forum like this makes an already brilliant programming language even better!