Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?

ASCII file

by rxd06040
on Jan 26, 2012 at 18:40 UTC ( #950190=perlquestion: print w/replies, xml ) Need Help??
rxd06040 has asked for the wisdom of the Perl Monks concerning the following question:

CAN ANY ONE HELP ME HOW TO WRITE THIS PROGRAM? Write a program to read any small ASCII (simple text) file and to count the frequency of each character present in the file. Print out each character and it's frequency of occurrence.

Replies are listed 'Best First'.
Re: ASCII file
by MidLifeXis (Monsignor) on Jan 26, 2012 at 18:56 UTC

    See perldoc -q frequency (search for frequency in perlfaq6) from your command line.

    Once you have tried out some of the things there, come on back and ask specific questions.


Re: ASCII file
by toolic (Bishop) on Jan 26, 2012 at 18:55 UTC
    What part are you having difficulty with? Read some docs: Write some code, and post again if you still have specific questions.
Re: ASCII file
by pemungkah (Priest) on Jan 26, 2012 at 22:17 UTC
    Here's how I would write this program:

    First, figure out what I would do if I wasn't using a computer:

    1. get the file in front of me.
    2. look through the file character by character.
    3. for every character, make a tickmark on another sheet of paper next to the character's name, adding new characters as I saw them.
    4. Copy the table I generated to another sheet of paper in the proper order.
    So you're going to want to do pretty much the same thing using the computer. Instead of pieces of paper, you'll use data structures to hold the information as you assemble it. For this problem, you'll want to be able to look at the characters one by one, so you'll need to read up on open() (to be able to read the file at all), the angle-bracket operator <$fh> to read a line of input, and the split() function to break it up into individual characters.

    Next, you'll need a table that maps one thing to another (a character name to a count), so you'll probably want to read up on hashes and how they work, since hashes let you map a key (in this case a character) to some other value (in this case a count).

    You'll want to loop over the input while there is some, so read up on while loops.

    Finally, you'll want to print things out in order, so you'll want to look at the Perl FAQ to see if there's a hint on sorting a hash.

    From here, it's a matter of putting the pieces together and creating the final program. This is really all programming is: looking at what you want to do, and mapping that into the tools you have to do it with. The creative part of programming is realizing how you can use your tools better, faster, or more simply to do the job.

    Note that this is not a program, but an insight into how to think about a problem so you can write a program, which is the solution you actually need - it's not always possible to find someone who can whip a program out for you. This time, there was an answer - but it's not always that simple. If you teach yourself how to think about problems so you can break them down into pieces that you know work, then it's "easy" to program. Perl is really good for this as with CPAN there is an immense toolbox of stuff that is known to work for bigger problems: parsing XML, fetching pages from the Web, etc.

Re: ASCII file
by Anonymous Monk on Jan 26, 2012 at 19:20 UTC
    perl -MDevel::Dwarn -MList::UtilsBy=count_by \ -e 'Dwarn({ count_by { $_ } map split("", $_), <> });'
Re: ASCII file
by JavaFan (Canon) on Jan 26, 2012 at 21:47 UTC
    use autodie; open my $fh "<", "ASCIIFILE"; my @c; while (<$fh>) {$c[ord]++ for split //} for ($_ = 0; $_ < @c; $_++) { printf "'%s' occurs %d times\n", chr, $c[$_] if $c[$_]; }
Re: ASCII file
by thundergnat (Deacon) on Jan 27, 2012 at 17:20 UTC
    perl -E"undef$/;map{$a{$_}++}split//,<>;say qq/$_ => $a{$_}/for sort k +eys%a" test.txt
Re: ASCII file
by chessgui (Scribe) on Jan 26, 2012 at 18:54 UTC
    open ASCII,'test.txt'; @chars=split //,join('',<ASCII>); %char_hash=(); foreach $c(@chars) { $char_hash{$c}++; } foreach $c(keys(%char_hash)) { print "$c occurs $char_hash{$c} times\n"; }
      That's not "help" for rxd06040; that's "do it for" the OP.

      Please don't. You undermine the Monastery by doing, rather than teaching. See the replies,Re: ASCII file and Re: ASCII file from MidLifeXis and toolic that actually "help" without encouraging -- if not the OP, others, who think their "gimme's" constitute an acceptable question.

      So, you might ask, why didn't Anonymonk's offering draw this remark? Because that's a sufficiently unlikely way to code what OP's looking for to be satire ...or a honey-trap for those trying to have us do homework for them.

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (5)
As of 2018-02-22 01:22 GMT
Find Nodes?
    Voting Booth?
    When it is dark outside I am happiest to see ...

    Results (288 votes). Check out past polls.