Recent Articles

Perl 6 Progress Discussed
You may want to pull together some philosophy textbooks and a dictionary; all of them seem like useful resources in a new debate on whether or not Perl 6 is vaporware. Still, regardless of how you define...

February Perl Conference Planned
Make no mistake about how cold things will be during this conference; we're talking about Minnesota in early February, and the daily lows and highs should be...

Perltidy Offers Reformatting Help
Take a look at any car that's littered with empty coke bottles and mangled takeout bags. Or any desk that's covered with bent pieces of paper and a dozen redundancies in terms of office supplies.

Red Hat's Perl Problem Makes Waves
Whoops. Although Perl's not to blame, a problem involving it has come into the spotlight. Fingers are pointing at Red Hat over a screwup that made a Perl program run about 100 times slower than a...


12.02.08

Using Perl To Count Letter Occurrence In A Document

By Dave Taylor

Hey I want a Perl script that reads a file and sends me the number of occurrences of the alphabets in that file... Could you please help me?

Dave's Answer:

This is an interesting little puzzle so what I will do is show how to write a quick, short Bourne Shell script for Linux (or Mac OS X if you crack open your Terminal.app program) that can do what you seek.

The key idea is that if you could transform the input to be one- character-per-line, it'd be unreadable for humans, but would make it really, really easy to sort and tally for a computer program.

How do you do that? With one of what I call the unsung heroes of the Unix command line, the "fold" command. Generally, people use fold to wrap overly long lines in text files (it's great for processing info prior to printing it, for example) but as with all great Unix command line utilities, it has parameters that let you change its behavior. And that's just what we'll do.

The Fundamental Server: Everything You
Need Inside and Outside the Box - Learn More

Try this yourself:

$ date | fold -w3
Wed
Nov
26 0
9:56
:48
MST
2008

That's with width=4. Turn it into "-w1" and each and every character is on its own line. (I won't reproduce it here because it's crazy long and you get the idea anyway, I hope!)

Now that each character is on its own line, it's simple to sort the output to ensure that they're in alphabetical order with "sort". To tally matching lines turns out to be a feature of another of the unsung heroes, "uniq". Check its man page and you'll see:

Continue reading this article.

About the Author:
Dave Taylor has been involved with the Internet since 1980 and is internationally known as an expert on both business and technology issues. Holder of an MSEd and MBA, author of twenty books and founder of four startups, he also runs a strategic marketing company and consults with firms seeking the best approach to working with weblogs and social networks. Dave is an award-winning speaker and frequent guest on radio and podcast programs.

AskDaveTaylor.com
http://www.intuitive.com/blog/
About PerlProNews
PerlProNews is a collection of news and commentary designed to keep you in step with the ever evolving landscape of Perl environments. News and Advice for Perl Professionals





PerlProNews is brought to you by:

SecurityConfig.com NetworkingFiles.com
NetworkNewz.com WebProASP.com
PerlProNews.com SQLProNews.com
SysAdminNews DevWebPro.com
LinuxProNews.com WirelessProNews.com
CProgrammingTrends.com ITCertificationNews.com






-- PerlProNews
is an iEntry, Inc. publication --
iEntry, 2549 Richmond Rd. Lexington KY, 40509
2008 iEntry, Inc.  All Rights Reserved  Privacy Policy  Legal

archives | advertising info | news headlines | free newsletters | comments/feedback | submit article



Database Forum News and Advice for Perl Professionals PerlProNews News Archives About Us Feedback PerlProNews.com About Article Archive News Downloads WebProWorld Forums iEntry Advertise Contact Jayde