Perl - Part 21

[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]

Saving More Complex Data

In the program names04.pl you created a program that stored both names and telephone numbers. Previously you had only stored names.

Realistically, it would be unlikely that you would create a "real-world" program just to save names; and if you did, you'd undoubtedly expect to have a better means of recovering the names than expecting to remember the order in which they were entered. That being said, this was suitable for our first few programs.

Our next stage was to create a program that saved both names and telephone numbers. Initially we again expected to be able to recover a name and number by the order in which they were entered; we quickly changed that to enable us to recover a phone number using the name of the person.

Task 024

This, essentially, will be our task again. But since then we have learned how to write data to a disk file, as well as read back the contents of the disk file. To do this, we're going to try two different methods and see how they work out for us. First copy file names04.pl to names04a.pl. Then modify the file to look as shown below:

Now enter some data, consisting of names and numbers, like this:

Whatever data you enter, you can examine with the cat command as shown here:

Task 025

Having accepted the input, created the file and output the data, we need to be able to read that data back in to make use of it. This we can do by modifying the file names04.pl again. Copy it to names04b.pl and modify it as below:

In this program we want to read in the names and then the telephone numbers. However, when you run the program you'll find the output is probably similar to that show below:

Clearly we have an issue with how the data is being stored and retrieved. What is the problem?

Before moving on you should be able to volunteer some explanation as to why the data is not retrieved in the manner in which it was input, and in which it was confirmed (using cat) that it was saved.

Onwards...

[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: 20131015-16:45