Building Data Structures with Existing Data

The examples of creating nested data structures with anonymous data work great when you already know exactly what data that structure is going to contain ahead of time. But in real life, these sorts of structures tend to get built from data that might be read in from a file or entered via the keyboard.

In that case, sometimes you'll end up combining anonymous data with references to regular variables. As long as you end up with references in the right spot, there's no harm in using whatever mechanism works best for building your data structure. For example, let's say that you had a file that contained a matrix of numbers that looked something like this:

 3 4 2 4 2 3 5 3 2 4 5 4 7 6 3 2 8 3 3 4 7 8 3 ...

Get Sams Teach Yourself Perl in 21 Days, Second 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.