From the course: Perl 5 Essential Training
Unlock the full course today
Join today to access over 22,400 courses taught by industry experts or purchase this course individually.
Hash references - Perl Tutorial
From the course: Perl 5 Essential Training
Hash references
- [Voiceover] Hash references are extremely common in Perl. Perhaps even more common than named hashes. They are simple and convenient, and you'll probably use them often. Here is a working copy of hashref.pl from Chapter 11 of the Exercise Files. And you notice, beginning on line seven, I declare a hash in the normal way, and then I create a reference to the hash, here in line 15, and you notice I'm using the backslash reference operator to take a reference of the hash and assign it to the scalar $ref. Then I have a "foreach" loop, and I'm dereferencing the hash reference, and I'm using "sort" and "keys" to get a sorted list of the keys, and then I dereference an individual element using a key inside of the loop. So when I save this and run it, you notice I get a sorted list of the keys, so they are in the order five, four, one, two, three, because that's the alphabetical order of the strings, and we have the key and the value there in the result. Now, again, just like with the…
Practice while you learn with exercise files
Download the files the instructor uses to teach the course. Follow along and learn by watching, listening and practicing.