If you cannot clearly describe your data and your desired result, you cannot encode it - accepting a simple project is a good way to start working with a new language.
Let me introduce a simple method that you can use to extract code in any language, whether you know it or not. This method only works for small projects. You will need to actually plan large projects.
How to write a program:
- Open a text editor and write down what data you have. Make each line comments
- Describe the desired results.
- Start by describing the steps necessary to change your data in the desired form.
Figures 1 and 2 complete:
#!/usr/bin perl use strict; use warnings;
Now that you know what you have and where you need to go, you can determine what the program needs to do to get you there - this is step 3:
You know you need to have a list of fields, so get this first:
Since it is easiest to write CSV in a row-oriented style, you need to process all your files before creating each line. Thus, you need a place to store data.
Now we read the data files:
# Get a list of data files to parse # Iterate over list # For each data file: # Read the string of digits. # Assign each digit to its field. # Store data for later use.
We have all the data in memory, now write the output:
# Write the CSV file. # Open a file handle. # Iterate over list of fields # For each field # Get field name and list of values. # Create a string - comma separated string with field name and values # Write string to file handle # close file handle.
Now you can start converting comments into code. For each comment, there can be from 1 to 100 lines of code. You may find that something you need to do is very difficult, and you do not want to accept it at the moment. Create a dummy routine to handle a complex task and ignore it until you get everything else. Now you can solve this complex, thorny sub-problem yourself.
Since you're just learning Perl, you need to get into the docs to learn how to accomplish each of the subtasks represented by the comments you wrote. The best resource for this kind of work is a list of functions by category in perlfunc . The Perl syntax guide is also useful. Since you will need to work with a complex data structure, you will also want to read from the page data collection .
You might be wondering how you should know which perldoc pages you should read for this problem. An article about Perlmonks entitled How RTFM provides a good introduction to documentation and how to use it.
Best of all, if you're stuck, you have a code to share when you ask for help.