Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Extracting money from a double quoted string

by sherab (Scribe)
on Dec 06, 2014 at 14:38 UTC ( [id://1109407]=perlquestion: print w/replies, xml ) Need Help??

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

Hello monks, I have a HUGE amount of text being read into a double quoted string but in my example below I greatly shortened it. I am having a devil of a time just simply extracting the currency from this string. Single quoted it works just fine but the interpolation issues I am getting with a double quoted string are throwing me into fits. I tried quotemeta but perhaps I am using it wrong. Has anyone encountered this before? I'd appreciate any help in the right directions.
#!/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;

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

    Why don't you just single-quote your string?

    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";
      String is being slurped into a variable. Can you slurp into a single quote string?

        Your program that you posted above does not show any string slurping.

        All data read from a file is not reinterpreted. Data read from a file is treated similar to data in single quotes.

        Maybe now is a good time to post a program that shows what you are actually doing. If your real problem is with a program that reads data from a file, then please post such a program and a (short, but representative) file to read from. Also, please show the output you get, and the output you expect.

        When debugging how Perl sees data, it often helps me to print out the data right after having read it from a file:

        my $string= read_file('some_file.txt'); print "Read: [[$string]]\n";

        Update: Upon rereading your node, I think your problem is exemplified through your question:

        Can you slurp into a single quote string?

        Once a string has been read by Perl, be it from the source code or from a file, there is no distinction for Perl between "single quoted" and "double quoted" strings. Strings are plain values.

        The only moment where strings get treated differently by Perl is when they are created. Double-quoted strings in source code are subject to variable interpolation and escape character replacement (\n to newline, \t to tab, for example). Single-quoted string are only subject to some very limited escape character replacement (\\ to \, \' to '). Data read from a file is subject to neither. See Quote and Quote-like Operators for further reading.

        Can you slurp into a single quote string?

        The distinction between single quotes and double qoutes exists only with relation to string literals, i.e. strings that are in the source text of a script or module. I'd say that is the reason for our collective misunderstanding.

        That said, as a "shot into the blue", do you perhaps eval your string?

Re: Extracting money from a double quoted string
by BrowserUk (Patriarch) on Dec 06, 2014 at 14:50 UTC
    I have a HUGE amount of text being read into a double quoted string

    How do you "read into a double quoted string"?

    You can type into a double quoted string; but it would be a particularly inefficient way to deal with "a HUGE amount of text".


    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.

      Does this pass a syntax, compilation and execution test? Yes.

      Does it pass as a reasonable interp of op's use of "read" and/or a sanity test? I dunno!

      #!/usr/bin/perl use strict; use warnings; use 5.018; # read_to_dblquoted_str.pl my $str=qq("); my $txt = "abcdef \$3.05 xyz"; # to minimize, assigning this way, st +ed openning a file and reading it. $str .= $txt . qq("); say "\$txt: " . $txt; say "\$str: " . $str; =head C:\> read_to_dblquoted_str.pl $txt: abcdef $3.05 xyz $str: "abcdef $3.05 xyz" =cut

        That's not "a double quoted string" it's "a string containing double quotes".

        The difference is that a double quoted string in interpreted at compile time as a part of the program.

        What you've constructed at runtime is not. It's just a scalar that contains some characters.

        (Note:when you print a double quoted string; the quotes aren't printed. Their purpose is served by the time you get to runtime.)


        With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        "Science is about questioning the status quo. Questioning authority".
        In the absence of evidence, opinion is indistinguishable from prejudice.
Re: Extracting money from a double quoted string
by Laurent_R (Canon) on Dec 06, 2014 at 15:20 UTC
    The problem in the piece of code you showed is due to the way you are populating the string (Perl sees the $ sign as the beginning of the name of a variable. In the example you've shown, you could use single quotes to get rid of the problem, or you could escape the $ sign to give it its literal value and thereby preventing interpolation.

    But if you have a HUGE string, then it is presumably coming from a file or some other external source (STDIN, another program, socket, etc.). In that case, I do not see how you could encounter the problem you are describing.

    Please give some additional information on how you get this huge string populated.

      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?).

        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.

        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.

Re: Extracting money from a double quoted string
by GrandFather (Saint) on Dec 06, 2014 at 22:18 UTC

    sherab there seems to be a HUGE mismatch between what you describe and what we understand. We are using the same words, but seem to mean different things by them. See I know what I mean. Why don't you? then reconsider your question. Then:

    1. Frame your question around some code that is as near your real problem as you can make it using just a few lines of code and a small amount of data.
    2. Show us the output you get.
    3. Show us the output you want.

    Don't tell us what you think is happening because that hasn't seemed to help anyone.

    Perl is the programming world's equivalent of English

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (2)
As of 2024-04-19 21:01 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found