Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

String contents

by perlyr (Novice)
on Jun 29, 2012 at 07:05 UTC ( #979054=perlquestion: print w/ replies, xml ) Need Help??
perlyr has asked for the wisdom of the Perl Monks concerning the following question:

Hi, How do I print out the contents of a string, not just words and numbers, but also white space, newline, and anything in that string. Thanks,

Comment on String contents
Re: String contents
by Anonymous Monk on Jun 29, 2012 at 07:08 UTC

    Hi, How do I print out the contents of a string, not just words and numbers, but also white space, newline, and anything in that string. Thanks,

    print doesn't care what its printing, it will happily print anything

    Maybe you want to use Data::Dump?

      you are right, I wanted to print out the string structure. If I know the structure, then I could extract it. Any other suggestions?

        you are right, I wanted to print out the string structure. If I know the structure, then I could extract it. Any other suggestions?

        About what? Read How do I post a question effectively?

        use strict; use warnings; use Data::Dumper; #do some stuff that generates a structure called $struct->stuff print Dumper($struct->stuff);

        or if you want a structure more stringified, use Data::Dump as already mentioned

Re: String contents
by bitingduck (Friar) on Jun 29, 2012 at 07:10 UTC
    Have you tried looking in the docs (or searching anywhere) on "print" or "perl print"? It would have taken less time than posting here and waiting for a response. usually print $string; is a pretty good start...
      Yes, I looked the the documentation, but didn't find anything.
Re: String contents
by GrandFather (Cardinal) on Jun 29, 2012 at 08:02 UTC

    Maybe there is a bigger problem you are trying to solve than just "print a string"? If so perhaps you should express it so we can help with the real issue rather than everyone being frustrated because the obvious answer to the question you asked has no relevance to the solution you are looking for?

    True laziness is hard work
      Yes, I was trying to extract a string from a textfile, but couldn't parse it correctly. Here is part of the file:
      ended June 30, 2001 in conformity with accounting principles generall +y accepted in the United States of America. Also in our opinion, the related fina +ncial statement schedule, when considered in relation to the basic consolida +ted financial statements taken as a whole, presents fairly, in all materia +l respects, the information set forth therein. Melville, New York /s/KPMG LLP September 26, 2001
      Here is my regex:
      /^\s*(\w+),\s*(\w+ \w+)(.+?\s*LLP)$/m;

      I wanted to extract: Melville, New York, /s/KPMG LLP

      Any suggestions?

        Melville, New York /s/KPMG LLP

        September 26, 2001

        Any suggestions?

        Yes. Take note of my sig. I'm not going to work hard to decode your node because you are too lazy to read the instructions about using a little mark up so that your node is nicely formatted.

        If you had expended just a little time and effort (maybe five minutes) to explain your problem well and formatted your node nicely you'd have had an answer in 15 minutes and be an hour ahead.

        True laziness is hard work
Re: String contents
by davido (Archbishop) on Jun 29, 2012 at 08:06 UTC

    The problem that we all seem to be having with your question is that print prints the contents of the string already. If you want something different from what print does, you'll need to be more specific. Wanting to see "the structure of the string" is not explicit enough. Are you saying that you want whitespace converted to something more visible? Do you want one of those little arrow things everywhere a \n shows up? Do you want little dots in place of spaces, and long arrows in place of tabs? Do you want color highlighted sentence structure charts?

    Maybe you want to know all of the octets, dumped in hex format. Maybe you want HTML::Entities to encode your strings so that ampersands show up with the & encoding.

    Whatever it is that you want, you need to tell us exactly what it is. Otherwise, the best answer is print, because it prints the contents of a string.


    Dave

      Sorry about my earlier posts. Here is the regex in a nice format:
      /^\s*(\w+),\s*(\w+ \w+)(.+?\s*LLP)/m;
      my question now is: why did I extract "New" instead of "New York"? Any suggestions?

        My suggestion is to post in one node the sample text, and the regular expression that is failing to match where you expect it to. Post them well formatted, using the tips found in Writeup Formatting Tips, and be sure that you're posting actual full copy and pastes of the text and code that fail.

        When I test the text you provided, and the regexp you provided in the preceding node, I got the following results:

        Capture variables:

        • Digit Captures
          • $1 => Melville
          • $2 => New York
          • $3 =>                             /s/KPMG LLP
        • ${^PREMATCH}  => ended June 30, 2001 in conformity with accounting principles generally accepted
          in the United States of America. Also in our opinion, the related financial
          statement schedule, when considered in relation to the basic consolidated
          financial statements taken as a whole, presents fairly, in all material
          respects, the information set forth therein.
          
        • ${^MATCH}     => 
          Melville, New York                            /s/KPMG LLP
        • ${^POSTMATCH} => 
          September 26, 2001
          STR
        • $^N           =>                             /s/KPMG LLP
        • @- => (352,354,364,372)
        • @+ => (411,362,372,411)

        The text I used was exactly this:

        ended June 30, 2001 in conformity with accounting principles generally + accepted in the United States of America. Also in our opinion, the related fina +ncial statement schedule, when considered in relation to the basic consolida +ted financial statements taken as a whole, presents fairly, in all materia +l respects, the information set forth therein. Melville, New York /s/KPMG LLP September 26, 2001 STR

        And the regexp I used was exactly this:

        /^\s*(\w+),\s*(\w+ \w+)(.+?\s*LLP)/m

        Try it yourself with my regexp tester, here: Perl Regex Tester


        Dave

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (7)
As of 2014-07-31 07:12 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (245 votes), past polls