NodeReaper has asked for the wisdom of the Perl Monks concerning the following question:

  • Comment on Removing commas and dollar signs from a variable.

Replies are listed 'Best First'.
Re: Removing commas and dollar signs from a variable.
by Albannach (Monsignor) on Dec 21, 2001 at 23:44 UTC
    My choice would be the transliteration operator tr or y:
    my $money = '$123,456.78'; $money =~ tr/$,//d; print $money;
    Gives: 123456.78

    --
    I'd like to be able to assign to an luser

Re: Removing commas and dollar signs from a variable.
by Rich36 (Chaplain) on Dec 21, 2001 at 23:41 UTC
    This can be done by using substitutions.
    $var =~ s/\,//g; # Substitute all commas with nothing $var =~ s/\$//g; # Substitute all dollar signs with nothing
    I would suggest checking out the Tutorials section or Learning Perl to learn more about regular expressions.
    Rich36
    There's more than one way to screw it up...

      Why use all that regex processing power that you don't need? tr is a much better solution in this case.

      --
      <http://www.dave.org.uk>

      "The first rule of Perl club is you do not talk about Perl club."
      -- Chip Salzenberg

        I am missing the benefits/drawbacks of s/// vs. tr/// .
        is it something to truly consider or is it just "more correct" ?

        thanks!
        Good point. tr's not something I use much, but probably should. Thanks for the reminder.
        Rich36
        There's more than one way to screw it up...