Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re^2: Extracting money from a double quoted string

by sherab (Scribe)
on Dec 06, 2014 at 18:50 UTC ( [id://1109426]=note: print w/replies, xml ) Need Help??


in reply to Re: Extracting money from a double quoted string
in thread Extracting money from a double quoted string

Thanks Laurent. That's it exactly, I am populating the string from file slurp. So far your answer is the only one I have seen that reasonably even tries to answer my question and help me solve my problem. I am aware that variables try to interpolate in a "string" versus 'string' (happy nitpickers?).

Replies are listed 'Best First'.
Re^3: Extracting money from a double quoted string
by Corion (Patriarch) on Dec 06, 2014 at 18:58 UTC

    The code you posted already works for me, with the simple change of double to single quotes:

    #!/usr/local/bin/perl $string='Price is $9.99 on our website'; ($money) = $string =~ m/is\s\$([0-9]{1,2}\.[0-9]{2})\son/g; print $money; __END__ 9.99

    You can help us help you better by posting a short, self-contained program that we can use to reproduce your problem.

      You'll note that I pointed out in my original question that it works with ' '. Thanks

        Then let me repeat what I gave as an answer in my first reply to you:

        As an alternative, you could quote the dollar sign, so that Perl knows you don't mean the variable $9:
        my $string= "Price is \$9.99 on our website";

        What part of that answer does not apply to your problem?

        Is your question why $string does not contain the value 9.99 when you use double quotes? Is your question about the difference between double and single quotes in Perl? Is your question about what the variable $9 contains?

Re^3: Extracting money from a double quoted string
by Laurent_R (Canon) on Dec 06, 2014 at 23:36 UTC
    If you are slurping your data from a file, then you will not have any problem with variable interpolation, and the $ sign will not give you any trouble. It is only if you are creating the string yourself in your code with double quotes that this "problem" arises. If you read data from a file, no variable interpolation occurs, so that it is as if you had used single quotes.

    And this is also basically what Corion said in the first place, but maybe you did not understand fully how it applied to your question.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1109426]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (3)
As of 2024-03-29 15:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found