Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

Note that ; $matrix[4][4]; only fills out the first level with five elements and populates the last with an array reference. This is because the last subscript doesn't do anything. Just like

if ($foo[100000] == 1) { ... }
doesn't extend @foo to be 100001 elements. This is also why it gives a "useless use of array element in void context" warning.

The first level is extended though, but only filled with undefs except for the fifth element which becomes an array reference, which will be empty because of the reason above, looking like

[ undef, undef, undef, undef, [ undef, undef, undef, undef , undef ], ]
Using the element in some way, not just using the value, like
$matrix[4][4] = undef
would fill out the array reference at index four in the first level, but the other levels would still be untouched, so you'd have a "half-expanded" 2D array, looking like
[ undef, undef, undef, undef, [ undef, undef, undef, undef , undef ], ]
This is why Cody Pendant uses the for loop.

ihb

See perltoc if you don't know which perldoc to read!


In reply to Re^3: Initializing multidimensional arrays by ihb
in thread Initializing multidimensional arrays by pearle

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 goofing around in the Monastery: (6)
As of 2024-04-23 10:56 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found