Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

print only uppercase sentences

by lakssreedhar (Acolyte)
on Aug 04, 2013 at 12:45 UTC ( #1047790=perlquestion: print w/ replies, xml ) Need Help??
lakssreedhar has asked for the wisdom of the Perl Monks concerning the following question:

I have a file which has sentences like

HOW ARE YOU =>FINE How are You=>FINE HOW ARE You=>FINE
Need to print only the first sentence.

Comment on print only uppercase sentences
Download Code
Re: print only uppercase sentences
by rjt (Deacon) on Aug 04, 2013 at 12:47 UTC
Re: print only uppercase sentences
by zork42 (Monk) on Aug 04, 2013 at 13:00 UTC
    1. read rjt's very useful links

    2. one solution is:
    my $sentence = ...; if ($sentence eq uc $sentence) { ... }
Re: print only uppercase sentences
by Laurent_R (Parson) on Aug 04, 2013 at 14:24 UTC

    You can try this:

    print if /U/;

    I leave it to you to check the documentation on how to read your file.

      yes its working

        Watch out, though. It is working on the question you asked in the text of your post with the 3 examples you gave (because only the 1st sentence has a capital U), it is not a valid answer to the more general question in the title of your post. For this more general question, I would rather suggest something like this:

        print if $_ eq uc $_;

        Or yet simpler:

        print if $_ eq uc;

        UPDATE: following polypompholyx's answer, I just want to clarify that my first answer was sort of a practical joke, something that would work fine in the specific case you presented (the question as asked in the original post and the example), but would most probably not do what you really wanted, the reason being that the question was very poorly defined (an example is very rarely a good specification). When I saw that you seemed to have bought it, I felt compelled to correct this false impression, with the above post and a real solution. The two code snippets above probably do what you want (in the right context), my first post did *not*, it worked only in the specific case of your example. As polypompholyx said, my idea was to try to let you think about why it would work with your example, but most probably not do you are looking for. Think about it, an example is usually very useful, but it is never a substitute for a good spec.

        Unfortunately,

        print if /O/;

        will also 'work' on your sample data, and for the same reason. zork42's post is probably the answer to your short-term problem; but rjt's links, and thinking carefully about why Laurent_R's post is almost certainly *not* what you're after would probably be a lot more useful to you in the long-term.

Re: print only uppercase sentences
by jwkrahn (Monsignor) on Aug 04, 2013 at 19:29 UTC
    $ echo " HOW ARE YOU =>FINE How are You=>FINE HOW ARE You=>FINE" | \ perl -ne'print if /\A[[:upper:][:^alpha:]]+\z/' HOW ARE YOU =>FINE
Re: print only uppercase sentences
by marinersk (Chaplain) on Aug 05, 2013 at 16:33 UTC
    A regular expression should do the trick; but since there is more than one way to do it (TIMTOWTDI), I would point out that the use of uc as noted by a previous poster provides another way to confirm the letters in the string are all uppercase.

    If that's too vague, perhaps a peek at your code will help focus things nicely. :-)

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (3)
As of 2014-09-20 14:44 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (159 votes), past polls