How best to take a copy of a variable and edit it with s///?

Discussion in 'Perl Misc' started by Henry Law, Jul 6, 2008.

  1. Henry Law

    Henry Law Guest

    I frequently have the need to take copy of a variable and edit it (for
    printing or some such). I code it like this:

    my $ugly_variable = some_function(); # Or read from a file maybe
    my $nice_variable = $ugly_variable; # Take a copy ...
    $nice_variable =~ s/nasty/nice/g; # ... and then make it look nice

    Of course that works very well. But it doesn't look perlish, and it
    takes two statements. I'm looking for some one-shot construction, along
    the lines of this

    my $nice_variable = ($ugly_variable =~ s/nasty/nice/g);

    .... but if course that doesn't do what I want: $nice_variable ends up as
    the number of substitutions done by s///. But is there a neater, more
    succinct (but not hard to maintain) way of doing what I want? I'm sure
    I saw something that looked like my wrong example above, but it must
    have been different in some subtle way.

    Of course I Googled for an answer to this; it's not that I found
    nothing, more that I couldn't structure a query that returned anything
    useful.

    --

    Henry Law Manchester, England
     
    Henry Law, Jul 6, 2008
    #1
    1. Advertising

  2. Henry Law

    Dave B Guest

    Henry Law wrote:

    > I frequently have the need to take copy of a variable and edit it (for
    > printing or some such). I code it like this:
    >
    > my $ugly_variable = some_function(); # Or read from a file maybe
    > my $nice_variable = $ugly_variable; # Take a copy ...
    > $nice_variable =~ s/nasty/nice/g; # ... and then make it look nice
    >
    > Of course that works very well. But it doesn't look perlish, and it
    > takes two statements. I'm looking for some one-shot construction, along
    > the lines of this
    >
    > my $nice_variable = ($ugly_variable =~ s/nasty/nice/g);
    >
    > ... but if course that doesn't do what I want: $nice_variable ends up as
    > the number of substitutions done by s///. But is there a neater, more
    > succinct (but not hard to maintain) way of doing what I want? I'm sure
    > I saw something that looked like my wrong example above, but it must
    > have been different in some subtle way.
    >
    > Of course I Googled for an answer to this; it's not that I found
    > nothing, more that I couldn't structure a query that returned anything
    > useful.


    Since assignment returns a valid lvalue, you have to do this:

    ($nice_variable=$ugly_variable) =~ s/nasty/nice/g

    --
    D.
     
    Dave B, Jul 6, 2008
    #2
    1. Advertising

  3. Henry Law

    Henry Law Guest

    Dave B wrote:
    > Henry Law wrote:
    >
    >> I frequently have the need to take copy of a variable and edit it (for
    >> printing or some such). I code it like this:
    >>
    >> my $ugly_variable = some_function(); # Or read from a file maybe
    >> my $nice_variable = $ugly_variable; # Take a copy ...
    >> $nice_variable =~ s/nasty/nice/g; # ... and then make it look nice


    > Since assignment returns a valid lvalue, you have to do this:
    > ($nice_variable=$ugly_variable) =~ s/nasty/nice/g
    >


    Of course! It's so obvious now. Thank you.

    --

    Henry Law Manchester, England
     
    Henry Law, Jul 6, 2008
    #3
  4. Henry Law <> wrote:

    > I frequently have the need to take copy of a variable and edit it (for
    > printing or some such). I code it like this:
    >
    > my $ugly_variable = some_function(); # Or read from a file maybe
    > my $nice_variable = $ugly_variable; # Take a copy ...
    > $nice_variable =~ s/nasty/nice/g; # ... and then make it look nice
    >
    > Of course that works very well. But it doesn't look perlish, and it
    > takes two statements. I'm looking for some one-shot construction, along
    > the lines of this
    >
    > my $nice_variable = ($ugly_variable =~ s/nasty/nice/g);



    You have the parenthesis in the wrong place:

    (my $nice_variable = $ugly_variable) =~ s/nasty/nice/g;


    --
    Tad McClellan
    email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"
     
    Tad J McClellan, Jul 6, 2008
    #4
    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. Alex
    Replies:
    2
    Views:
    1,258
  2. Replies:
    26
    Views:
    2,145
    Roland Pibinger
    Sep 1, 2006
  3. David Sanders
    Replies:
    4
    Views:
    436
    David Sanders
    Oct 30, 2007
  4. Anthony Williams
    Replies:
    6
    Views:
    586
    James Kanze
    May 9, 2008
  5. Paw
    Replies:
    8
    Views:
    406
Loading...

Share This Page