Perl - Part 22
[home] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11] [12] [13] [14] [15] [16] [17] [18] [19] [20] [21] [22] [23] [24] [25]
Is it a bird? Is it a plane? No its a...nothing; just a darn Database.
Our problem in the last pair of programs was that we are dealing with a small database: because of this when dealing with the data we are going to have to apply consistency to what we do in terms of input and output.
Specifically, the first program wrote out the names and numbers alternately and in pairs (each name followed by the correct number).
When we went to read in the names and numbers we used a different method - the only one we currently know. The use of @all_the_names = <NAMESFILE>; inputs *all* file data into the array. That's the purpose of that construct: to get a whole file into an array in one line. So when the line @all_the_numbers = <NAMESFILE>; shows up the file is already considered to be 'empty' - everything has already been read from it. We could close and re-open the file and fill the numbers array that way, but that isn't a solution. That would put the same contents into the numbers array as are already in the names array.
The fact that the numbers array fills from a pre-read (or 'empty') file explains why when we run program names04b.pl we get a blank for every second value. The numbers array is empty.
That we are alternately getting names and numbers can be attributed to the first line that reads in the file contents. It reads the contents indiscriminately, names and numbers, into the names array. Simply calling the array after 'names' doesn't automatically limit the contents to names, or even to text.
Finally(ish), notice that though there are 5 names and 5 numbers that 10 pieces of information are *not* put on screen by program names04b.pl - since the data is printed to screen using a loop controlled by the value of $max which is preset to 5. Therefore, though there is more data in the names array it never gets to be put on screen. We will need to cope with this also. To see the significance of this, change the value of $max to 10, run the program and see the difference. (You might even try changing it to 15 and see the effect).
Dealing with the Database
What we need is a better method of accepting and storing the names and numbers, plus any other data we like; we then need to have an equivalent method to retrieve that data and re-construct it in the original order and fashion. We also need to recognise that we are not dealing with random bits of data. The name and number are each a field of a Database, and that each name/number pair is a record in that Database. These definitions are the sames as those you've been given in, well, Databases; so nothing new there then.
We can think of the structure of the record as being like this:
- START_DATABASE
- Other records
- START_RECORD
- FIELD: Name
- FIELD: Number
- Other fields may be present...
- END_RECORD
- Other records may be present...
- END_DATABASE
What we can do is save the data in a file using a flat file database structure. This means that each line will represent one record, and is a well established mechanism for solving the minor problem we face here, as the previous link will explain.
We shall read in the names, the numbers & and any other data. We shall join (or concatenate) the fields so they can be saved onto one line (one record). We will place between the fields a special character of our choosing before writing the records to disk. All of these will then be joined into one line with the concatenate ('.' or 'dot') operator.
Then, when we re-read the records this special character will enable us to split the records back from being one line into being separate fields again. Thus we will be able to store, reload and re-use the data as required.
Now to make the great plan real....
[home] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11] [12] [13] [14] [15] [16] [17] [18] [19] [20] [21] [22] [23] [24] [25]
Last updated: 20120108-16:46