Lists and Hashes

When I discussed how to initialize a hash, I hinted that hashes and arrays are somehow related. Whenever a hash is used in a list context, Perl unwinds the hash back into a flat list of keys and values. This list can be assigned to arrays, like any other list:

%Movies=( 'The Shining' => 'Kubrick', 'Ten Commandments' => 'DeMille',
        Goonies=> 'Spielberg');
@Data=%Movies;

At this point, @Data is an array containing six elements. (The even elements—zero included—are director's names, and the odd elements are movie titles.) You can perform any normal array operation on @Data, and then you can reassign the array to %Movies, as shown here:

%Movies=@Data;

Note

Perl stores hash keys in a seemingly random order, useful only to Perl. ...

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