Try this:
use Modern::Perl;
use Data::Dump qw /dd/;
my @array_2d;
push @array_2d, do {chomp; [split ',']} while (<DATA>);
dd(@array_2d);
__DATA__
71,22,15,10,51
91,82,28,11,91
11,72,37,58,20
21,42,63,24,16
81,32,53,54,42
Printing your 2D array is done much easier, less error prone and more "perlish" as follows (if you do not want to use the
Data::Dump or
Data::Dumper functions):
for my $row_ref (@array_2d) {
for my $item (@$row_ref) {
print "$item ";
}
print "\n";
}
Actually your print routine contains an error: it only works because you have the same number of columns as rows! Do you spot your error?
CountZero
A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James
My blog:
Imperial Deltronics
-
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.