Q&A

Q1:Can I store more than one thing in a hash—for instance, if I need to store several things (a list) by one key?
A1: Yes. Two basic methods are involved. The first (and most cumbersome) is to format the value portion of the hash element into something recognizable, such as a comma-separated list. Whenever you store the hash element, you assemble the list into a scalar by using join, and whenever you retrieve a value from a hash, you split the scalar back into a list by using split. This method is cumbersome and error-prone.

The other method is to use a reference. References allow you to create hashes of arrays, hashes of hashes, and other complex data types. Using references to create complex structures is easy when you get the hang of it. ...

Get SAMS Teach Yourself Perl in 24 Hours THIRD EDITION 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.