Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight

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

Need to print only the first sentence.

Replies are listed 'Best First'.
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 (Abbot) 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


        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.

        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.

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 (Priest) 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?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1047790]
Approved by rjt
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (8)
As of 2017-06-28 13:27 GMT
Find Nodes?
    Voting Booth?
    How many monitors do you use while coding?

    Results (638 votes). Check out past polls.