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

Re: ASCII file

by pemungkah (Priest)
on Jan 26, 2012 at 22:17 UTC ( #950228=note: print w/ replies, xml ) Need Help??


in reply to ASCII file

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.


Comment on Re: ASCII file

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (6)
As of 2014-10-25 12:22 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (143 votes), past polls