http://www.perlmonks.org?node_id=366431

dfaure has asked for the wisdom of the Perl Monks concerning the following question: (strings)

Is it possible to assign a value to a variable and then do a substitution on it in a single statement? For example suppose I want to start with a URL, remove a known host-domain name, then assign the remaining dir/filename to a variable. With two statements you could say:

$dirfilename = $url; $dirfilename = s|http://foo.bar.com/||;

Originally posted as a Categorized Question.

Replies are listed 'Best First'.
Re: How do I assign & substitute in one statement?
by dfaure (Chaplain) on Jun 14, 2004 at 07:16 UTC

    From Re: Assign & substitute in one statement? by Zaxo

    Just write parens in their precedence-adjusting role:

    ($dirfilename = $url) =~ s|\Qhttp://foo.bar.com/\E||;

    From the Perl Cookbook, recipe 6.1.Copying and Substituting Simultaneously:

    You can even use this technique on an entire array:

    @bindirs = qw( /usr/bin /bin /usr/local/bin ); for (@libdirs = @bindirs) { s/bin/lib/ } print "@libdirs\n"; __OUTPUT__ /usr/lib /lib /usr/local/lib

    The parentheses are required when combining an assignment if you wish to change the result in the leftmost variable. Normally, the result of a substitution is its success: either "" for failure, or the number of times the substitution was done. Contrast this with the preceding examples where the parentheses surround the assignment itself. For example:

    ($a = $b) =~ s/x/y/g; # copy $b and then change $a $a = ($b =~ s/x/y/g); # change $b, count goes in $a
Re: How do I assign & substitute in one statement?
by Roy Johnson (Monsignor) on Nov 13, 2004 at 22:22 UTC
    Predicate-for allows you to declare and modify a lexical in the same statement:
    s[http://foo.bar.com/][] for (my $dirfilename = $url);
    Using block-for does not put the lexical in the surrounding scope. This is an interesting difference, particularly since -MO=Deparse indicates that the above statement is translated into the block form.
Re: How do I assign & substitute in one statement?
by dimar (Curate) on Nov 13, 2004 at 03:10 UTC

    You can also use parenthesis to impose 'list context', and then combine that with 'map' to get what you want in a single line. The drawback is you have to shoehorn your string into a list, which is a bit artificial. The benefit is this may be a little bit easier to read and understand what is going on for beginning perl programmers.

    $sBegin = "hello world"; ($sEnd) = map{s/hello/goodbye/g;$_;}($sBegin); print $sEnd;
      This is not unlike the earlier for solution. Using for instead of map avoids the clunky returning of $_:
      $sBegin = 'hello world'; s/hello/goodbye/g for ($sEnd = $sBegin); print $sEnd;

      Caution: Contents may have been coded under pressure.
      Important thing I overlooked: your solution modifies $sBegin!

      You would need to create a copy before modifying. One way:

      ($sEnd) = map {s/hello/goodbye/g; $_;} map {$_} ($sBegin);
      Another:
      ($sEnd) = map {s/hello/goodbye/g; $_;} @{[$sBegin]};
      A functionally similar solution that doesn't use map:
      $sEnd = do {local $_ = $sBegin; s/hello/goodbye/g; $_;};
      Of course, creating a copy and working on it is the canonical solution:
      (my $sEnd = $sBegin) =~ s/hello/goodbye/g;
      This couldn't be used in a combination declaration-assignment, though, while the others could.

      Caution: Contents may have been coded under pressure.
      Those parentheses do not impose list context. Parentheses never by themselves make any change to the context of what is in them.