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

Im wondering how, using the s// operator you can actually change the data in a string without having to only evaluate the $_ variable. Examples:

with split:
$blah = split(/\W+/ /,$blah2);

how would this be done with s//?

Replies are listed 'Best First'.
Re: Quick Regex question
by swiftone (Curate) on Nov 18, 2000 at 02:34 UTC
    Bind the string to the operation using =~ (see perlop):
    $blah2 =~ s/\w+//;

    Update: Note this doesn't prevent you from doing this:

    my ($word) = $string =~ /(\W+)/;
    But you should be careful about maintainability. This trick appears to only work with the m// construct in list context.
      Mistake. sorry. This node scheduled for removal (hopefully!).
Re: Quick Regex question
by Nimster (Sexton) on Nov 18, 2000 at 02:35 UTC
    try the =~ operation syntax string buzzword (was in original post: thingy)
    $blah=~s/\W+/$blah2/

    just had to be the first to comment, but I missed it anyway. Didn't want to be left behind when swiftone started editing his!
    This doesn't prevent you from doing

    my ($something) = my ($something2) = $blah =~s/\W+/$blah2/

    Beat that, swiftone! :)

    -Nimster

Re: Quick Regex question
by damian1301 (Curate) on Nov 18, 2000 at 02:56 UTC
    $one = "<HTML><Just testing this thing>"; $one =~ s/\W+/ /;
    for all u people that wanted to see my test project thingamajig, here it is.
      I'll post a nice verbose post to elucidate this for everyone not involved in the chatterbox:

      You wanted to replace all non-word characters with spaces. The pattern you listed wasn't working.

      The problem was that you didn't use the /g modifier, so it only replaced the first matching expression.

        In that case, I wouldn't use s/// at all. tr/// is faster and fits better, conceptually.

        tr/a-zA-Z0-9/ /c;

        I really like that /compliment switch.