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

Morning,
I am writing an email script which is called with a $sender and $recipient variable. It then runs one of several commands based on a pattern in the $recipient variable (eg... user-spam@test.com or user-ham@test.com). Because I use the un-altered email address to track statistics I need to strip the -spam and -ham part out and pass the result on as a variable for use later in the script. Any thoughts? -stephen

Replies are listed 'Best First'.
Re: Stripping a pattern from a variable
by brian_d_foy (Abbot) on Mar 24, 2005 at 07:40 UTC

    Save the unaltered version in another variable, then munge it. End up with both versions at the end. The regex you use in the substitution depends on the real situation, but everything else should be basically the same.

    #!/usr/bin/perl while( <DATA> ) { chomp; my $munged = $_; $munged =~ s/-(?:spam|ham)(?=@)//; printf "%-30s --> %-30s\n", $_, $munged; } __END__ joe@example.com joe-ham@example.com joe-spam@example.com joespam@example.com wortham@example.com
    --
    brian d foy <bdfoy@cpan.org>
      I am all set, thank you.
    A reply falls below the community's threshold of quality. You may see it by logging in.
Re: Stripping a pattern from a variable
by PodMaster (Abbot) on Mar 24, 2005 at 07:35 UTC
    Any thoughts?
    What's stopping you from doing it?

    I might use Email::Address to get at the user name portion ...

    MJD says "you can't just make shit up and expect the computer to know what you mean, retardo!"
    I run a Win32 PPM repository for perl 5.6.x and 5.8.x -- I take requests (README).
    ** The third rule of perl club is a statement of fact: pod is sexy.