There's more than one way to do things | |
PerlMonks |
comment on |
( [id://3333]=superdoc: print w/replies, xml ) | Need Help?? |
Your are right, you can't really place an array inside another array with Perl, but you can put in an array a reference toward another array. And you can do it in a way that you don't have to be explicit about the existence of a reference, it really looks like a two-dimensional array. Suppose you want to store in an array the full name and the abbreviated name of each of the months. Your could do this in various syntactic ways as follows: If you dump the content of @month under the debugger, you get the following structure:
The dump shows that the @month array contains 4 references to arrays (the 'ARRAY(0x20381390)' part), and that each of these arrays contain two elements, the month full and short names. And you can just print one element as follows: print $month[3][1]; # prints "Apr"So you could almost use all this without even knowing about references. Except that if you don't understand the underlying references, you will not be able to understand what's happening when something goes wrong (say, when the compiler complains about some illegal construct or, worse, when your program is not doing what you want because your data is not quite what you think). In brief, you can in principle construct an entire program with AoA (or AoH, HoA or HAH) without ever using a syntax that explicitly shows that the top-level array contains in fact references to other (anonymous) arrays. But you still need to know and understand this reference shebang if you are going to use it for anything else than an extremely simple program. In reply to Re^3: Creating a unique variable type
by Laurent_R
|
|