new variable stupidity

Discussion in 'Perl Misc' started by Time Waster, Feb 23, 2008.

  1. Time Waster

    Time Waster Guest

    OK, i'm being lazy to ask here. :(

    I'm always finding myself doing this:
    $newvar = $oldvar;
    $newvar = s/something/else/;
    ...because i want both of them. Is there a way to do this in 1 statement?
     
    Time Waster, Feb 23, 2008
    #1
    1. Advertising

  2. Time Waster <> wrote:
    > OK, i'm being lazy to ask here. :(
    >
    > I'm always finding myself doing this:
    > $newvar = $oldvar;
    > $newvar = s/something/else/;
    > ..because i want both of them. Is there a way to do this in 1 statement?



    (my $newvar = $oldvar) =~ s/something/else/;


    --
    Tad McClellan
    email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"
     
    Tad J McClellan, Feb 23, 2008
    #2
    1. Advertising

  3. Time Waster

    Ben Morrow Guest

    Quoth :
    > On 2008-02-23, Tad J McClellan <> wrote:
    > >
    > > (my $newvar = $oldvar) =~ s/something/else/;
    > >

    > I thought that would change $oldvar as well, but it doesn't. Why not?


    The return value of (my $newvar = $oldvar) is $newvar, so that's what
    gets modified.

    Ben
     
    Ben Morrow, Feb 25, 2008
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Roman Werpachowski
    Replies:
    6
    Views:
    298
    Markus Becker
    Jan 2, 2006
  2. Replies:
    3
    Views:
    412
    Lasse Reichstein Nielsen
    Sep 6, 2008
  3. andrew cooke
    Replies:
    1
    Views:
    282
    Chris Rebert
    Apr 20, 2011
  4. trans.  (T. Onoma)
    Replies:
    21
    Views:
    246
    Kristof Neirynck
    Jan 18, 2005
  5. Zachary Buckholz
    Replies:
    11
    Views:
    169
    G Klinedinst
    Jan 17, 2004
Loading...

Share This Page