Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

How do I assign & substitute in one statement?

( #366431=categorized question: print w/ replies, xml ) Need Help??
Contributed by dfaure on Jun 14, 2004 at 07:09 UTC
Q&A  > strings


Description:

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/||;

Answer: How do I assign & substitute in one statement?
contributed by dfaure

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
Answer: How do I assign & substitute in one statement?
contributed by Roy Johnson

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.
Answer: How do I assign & substitute in one statement?
contributed by dimar

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;

Please (register and) log in if you wish to add an answer



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others avoiding work at the Monastery: (7)
    As of 2014-07-14 04:11 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      When choosing user names for websites, I prefer to use:








      Results (254 votes), past polls