CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
Substitute function with special characters

 



bcgrue
Novice

Oct 30, 2014, 2:21 PM

Post #1 of 4 (4422 views)
Substitute function with special characters Can't Post

I am trying to write my very first perl script (perl v 5.8.4, OS:Solaris )

My goal is reading currency value( e.g., $192.08 or ($132.07)) from csv file and passes it to sub routine to get it returned with number only format (eg. 192.008, -132.07)
I have been trying to use substitute function in the sub routine like below:
$reformatteed_value =~ s/$//;
$reformatteed_value =~ s/(/-/; # replace open parenthesis with minus
$reformatteed_value =~ s/)//;

However, I am either getting unexpected result or syntax error for parenthesis.
It looks like this substitute function does not like "$" or "()". How can I format the currency to number only?

Thanks for your help in advance


BillKSmith
Veteran

Oct 30, 2014, 3:54 PM

Post #2 of 4 (4419 views)
Re: [bcgrue] Substitute function with special characters [In reply to] Can't Post

These characters all have special meaning in a regular expression. You can tell perl to treat them as literal characters by "escaping" (preceding) each with a backslash character (\). This will work as you intend as long as your .csv file does not use any of those characters for anything else. It is safer to explicitly test for the currency value. It is easier to do the negative values as a special case.

Code
use strict; 
use warnings;
my @test_cases = ('$192.08', '($132.07)');
foreach my $value (@test_cases) {
print currency_value($value), "\n";
}

sub currency_value {
my $value = shift;
$value =~ s/\(\$(\d{1,3}\.\d\d)\)/-$1/x;
$value =~ s/ \$(\d{1,3}\.\d\d) /$1/x;
return $value;
}


Note:

It is important to do the negative first.

I as assuming values between ($999.99) and $999.99
Good Luck,
Bill


Zhris
Enthusiast

Oct 30, 2014, 8:50 PM

Post #3 of 4 (4414 views)
Re: [bcgrue] Substitute function with special characters [In reply to] Can't Post

Just to add to the confusion of escaping characters in regular expressions, as well as escaping certain characters to hinder their special meaning, on other occasions you need to escape certain characters to use their special meaning. In Bill's example, the d's have been escaped in order to use their special meaning of "digit between 0 and 9", otherwise they would be literal d's. A general rule is, if you are dealing with anything in the ranges of 0 to 9, a to z and A to Z, escape the character if you want its special meaning, otherwise if you are dealing with anything else, escape the character if you want its literal meaning, but of course escaping a character that has no alternative special meaning in regular expressions is unnecessary. An alternative to explicitly backslashing characters that is particularly useful when you have a long string that you want to be taken literally, is to run the string through the built-in quotemeta function beforehand, or wrap the string with \Q and \E in your regular expression.


Code
/$/      - special meaning: end of line anchor 
/\$/ - literal meaning: dollar
/\Q$\E/ - literal meaning: dollar
/\Q\$\E/ - literal meaning: backslash followed by dollar
/\\$/ - mixed meaning: literal backslash followed by special end of line anchor

/d/ - literal meaning: letter d
/\d/ - special meaning: digit 0 to 9
/\Qd\E/ - literal meaning: letter d (unnecessary quotemeta)
/\Q\d\E/ - literal meaning: backslash followed by letter d
/\\d/ - literal meaning: backslash followed by letter d


Chris


(This post was edited by Zhris on Oct 30, 2014, 9:09 PM)


bcgrue
Novice

Nov 3, 2014, 6:56 AM

Post #4 of 4 (4218 views)
Re: [Zhris] Substitute function with special characters [In reply to] Can't Post

Thank you all for your responses. The code worked like a charm and the description on when and how to use escape was very helpful. I really appreciate for your help!!

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives