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

Monks: What is the best way to remove a comma and period from an amount variable? I tried s/,|.//;
my $amount = substr($source, -15); $amount =~ s/,//;
Thanks, Dave

Replies are listed 'Best First'.
Re: Removing Commas and Period in Amount Field
by eff_i_g (Curate) on Feb 10, 2009 at 22:01 UTC
    $amount =~ s/[.,]//g;

    If you only want digits: s/\D+//g;
      Great that worked. Here's another question. How can I pad a variable with zeros going from right to left? I used chomp on the sub string but it removes the values. Anyone have any ideas?
      my $checknum = "0000000" . substr($source, 18, 12);
      Thanks, Dave
        Maybe this is what you're looking for:
        # left-pad with zeros, total length of 21 my $checknum = sprintf("%021d", $source);

        See sprintf.
Re: Removing Commas and Period in Amount Field
by GrandFather (Saint) on Feb 10, 2009 at 22:19 UTC

    Define "best". The usual way of removing all of some set of characters from a string is to use tr///d.

    If you want to use a regex you probably want to include the g switch to have all matching strings replaced rather than just the first one.


    Perl's payment curve coincides with its learning curve.