Useful Hash Examples

If you have not seen hashes before, you might be amazed at the places where they can be used. The following examples show some application areas where this data type is useful. Notice in these examples that the hashes are built dynamically in response to the requirements of the program. This is different from the %groups hash above, where that hash was initialized before accepting user input.

Counting Words in a File

This example demonstrates how to use a hash to count the frequency of words in a file. The strategy is to read lines from a file and split each line into its words. Then each word can be used as an index into a hash where the associated value can be the count for each word. Here is the code. See the folder

Get Programming PERL in the .NET Environment now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.