Документация
HTML CSS PHP PERL другое

Workshop

 
Previous Table of Contents Next

Workshop

Quiz

1:

Why would name have been an inappropriate search key for the Customer program you wrote in this hour, especially if the customer list were much, much longer?

  1. Combinations of last name and first name would have been more than Perl's hashes would allow.

  2. People's names are not unique keys.

  3. No one would ever want to search a customer database by name.

2:

What's the difference between an associative array and a hash?

  1. There is no difference.

  2. Associative arrays are used on more formal data sets, such as billing records.

  3. Hashes aren't really associative arrays in Perl, so they have a different name.

3:

What kinds of data are best suited for hashes?

  1. Simple lists of items

  2. Potatoes and corned beef

  3. Lists of key-value pairs

Answers

A1:

b. Perl's hashes are virtually unlimited in size, and users will (inevitably) ask for a search by name. But people's names are inappropriate, because they're not unique. The phone book has many, many duplicate names such as John Smith and Robert Jones.

A2:

a. Hashes and associative arrays are identical. The only difference is that hash is much easier to say and to spell.

A3:

c. Choice c is the correct answer, although a nice corned-beef hash can really round out breakfast.

Activities

  • Modify the Customer program to allow a search by name. Because you cannot use name as a hash key, you need to search through the values in the hash.

  • Modify the Customer program to allow searching by a partial key (for example, part of a phone number or part of an email address). You can use regular expressions to search for the patterns. Simply keep in mind that you could find multiple results and you should return each one.

    Previous Table of Contents Next
    © 2000- NIV