Beefy Boxes and Bandwidth Generously Provided by pair Networks
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:

$month[0][0] = "January"; $month[0][1] = "Jan"; $month[1] = ["February", "Feb"]; push @month, ["March", "Mar"]; push @{$month[3]}, "April"; push @{$month[3]}, "Apr";
If you dump the content of @month under the debugger, you get the following structure:
0 ARRAY(0x20381390) 0 'January' 1 'Jan' 1 ARRAY(0x20381528) 0 'February' 1 'Feb' 2 ARRAY(0x203815d0) 0 'March' 1 'Mar' 3 ARRAY(0x204424d0) 0 'April' 1 'Apr'

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
in thread Creating a unique variable type by tfredett

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (6)
As of 2024-04-20 00:19 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found