Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

How do I assign & substitute in one statement?

by dfaure (Chaplain)
on Jun 14, 2004 at 07:09 UTC ( [id://366431]=perlquestion: print w/replies, xml ) Need Help??

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.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://366431]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (3)
As of 2024-03-29 02:24 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found