Learn how to use hashes in Perl.
- [Voiceover] Hashes, sometimes called associative arrays, are very powerful and very common in Perl. Here's a working copy of hash.pl from Chapter four of the exercise files. A hash is a special kind of an array that associates keys and values. You'll notice that the hash variable is introduced with a % and it's defined using a bare word key. Of course you can put it in quotes if you need to include whitespace in it, but a bare word is allowed there, followed by an association symbol, which is two characters: an = and a > and then a value, which in this case is a string, but it can be any value, and then a comma.
Each of these key value pairs are separated by commas, and it's all enclosed in parentheses. So if I save and run this, you see that we get these values. We have a while list here, a while loop, and it defines two variables and assigns them the value from the hash. This each operator is just getting each successive value from the hash. The hash itself is not stored in any particular order, so you'll notice that our result is four, three, two, one, five.
It's kind of scrambled, and it may even be different on your system. In fact, the next time I run it, it may be different. You notice that it is: four, two, one, five, three. It's in a different order each time I run it. The hash is not stored in any particular order. The each operator returns a list of two values from the hash: the key and the value for the next element in the hash. This is commonly used to iterate a hash loop.
Originally, hashes were called associative arrays. You'll sometimes hear that term. In time, the terminology was updated to the simpler hash, and the term hash is more common in other languages as well. Technically, this structure uses what's called a hash table to translate the key to an index. This provides a number of important optimizations and properties. Keys may be searched quickly and efficiently. Keys are not naturally ordered, and keys are unique and duplicates are not allowed.
If I want to update a particular value in the hash, I can do so like this. $hash, you'll notice I'm using $ because this is operating on one value: the element that has the key number six. So I'm adding an element here, and I'm going to say seis. This will add an element to the hash with the number six key and when I run it, you notice that we have one now with a 6, or rather a string six key, and a string value that's seis, which I'm pretty sure is how you spell that in Spanish.
It adds an element to the hash. I can update an existing element like this. When I save and run, you'll notice that one is now foo rather than uno. I can delete an element. Let's delete element three. When I run, you notice now we just have four elements and three is missing. If you want to sort a hash, you can use the built-in sort function, and we'll cover sort in more detail later in this course, but you can do something like this.
Space this out a little bit to make it a little more readable. I can get the value for an individual key like that and then I can use the same say that I was using before. When I run this, save and run, you notice that these are now in alphabetical order. Obviously they're not in numerical order because these aren't numbers; they are strings, so they're in alphabetical order. You notice also here that I'm subscripting the hash with a variable.
Again, it doesn't need to be a string, it doesn't need to be a bare word string. It can be any value. Bare words strings are uniquely allowed in that context. Hashes are extremely useful and common in Perl. We'll see a lot more examples of hashes as we continue the course.
Watch to learn the details of the Perl syntax, from variables, conditionals, loops, and data structures to regular expressions, functions, and references. A quick-start guide is included for experienced developers who want to get up and running with Perl 5 fast, and the entire course is recommended for both new and experienced programmers alike. Later chapters cover file handling and reusing code with Perl modules, plus Perl best coding practices.
- Understanding Perl's general syntax and the anatomy of a Perl script
- Writing statements and expressions
- Creating assignments
- Working with variables and strings
- Using data types effectively
- Defining logical flow with conditionals and loops
- Using special variables
- Using Perl operators
- Performing simple Perl programming tasks with expressions
- Matching data
- Defining and calling functions
- Using references
- Handling files in the file I/O
- Using built-in functions
- Reusing code with modules
- Coding with Perl best practices
Skill Level Intermediate
Programming Foundations: Refactoring Codewith Simon Allardice1h 44m Intermediate
1. Setting Up
About Perl3m 36s
2. Quick Start
3. Basic Syntax
4. Values and Variables
7. Special Variables
9. Regular Expressions
11. References and Structures
12. File I/O
13. Built-In Functions
15. Best Practices
- Mark as unwatched
- Mark all as unwatched
Are you sure you want to mark all the videos in this course as unwatched?
This will not affect your course history, your reports, or your certificates of completion for this course.Cancel
Take notes with your new membership!
Type in the entry box, then click Enter to save your note.
1:30Press on any video thumbnail to jump immediately to the timecode shown.
Notes are saved with you account but can also be exported as plain text, MS Word, PDF, Google Doc, or Evernote.