Ok, I will repost the code, with lots and lots of comments. I changed the code just a little to maybe make it easier to read. If you still have questions, keep asking, I don't mind :)
# -------------------------------------------------
# Read an array from a file
# -------------------------------------------------
# ask user for filename
print "filename? ";
my $file = <>;
chomp $file;
# ask user for number of rows
print "How many rows in your file? ";
my $rows = <>;
chomp $rows;
# ask user for number of columns
print "How many columns in your file? ";
my $cols = <>;
chomp $cols;
# read my file
my $2d = readdt2($file,$rows,$cols);
# print output
use Data::Dumper;
print Dumper $2d;
# just want to now what the 5th row, 3rd column is
print $2d->[4][2],"\n";
# -------------------------------------------------
# subroutine to read a generic 2d array from a file
# -------------------------------------------------
sub readdt2 {
# get the name of the file to read
my $ifn = shift;
# get the number of rows of the array you want to read
my $I = shift;
# get the number of columns you want to read
my $J = shift;
# open the file (note that it is commented out for now for testing)
# open(my $IFH, "<$ifn") or die "cannot open file $ifn\n";
my @ret;
# read $I number of lines from your file (0 to $I-1)
foreach my $i (0 .. $I-1) {
# read one line from your file
# (for testing purpose, read data following the __DATA__ statement
+ below)
my $line = <DATA>;
# take your line from your data file, and split into an array, usi
+ng spaces as your deliminator
my @tmp = split(/\s+/,$line);
# take the first $J columns from your array,
my @columns = @tmp[ 0 .. $J-1 ];
# and make your i(th) row point to your array of columns
$ret[$i] = \@columns;
}
# return your array of arrays (i.e. a 2-d array)
return(\@ret);
}
__DATA__
11 12 13 14 15 16 17
21 22 23 24 25 26 27
31 32 33 34 35 36 37
41 42 43 44 45 46 47
51 52 53 54 55 56 57
61 62 63 64 65 66 67
If you want to be more generic, for example, to let the file determine the number of rows and columns, you could try this subroutine, which does not require you to specify the rows and columns
# -------------------------------------------------
# subroutine to read a generic 2d array from a file
# -------------------------------------------------
sub readdt2 {
# get the name of the file to read
my $ifn = shift;
# open the file (note that it is commented out for now for testing)
# open(my $IFH, "<$ifn") or die "cannot open file $ifn\n";
my @ret;
# keep track of row number
my $i = 0;
# read from your file
# (for testing purpose, read data following the __DATA__ statement b
+elow)
while (my $line = <DATA>) {
# take your line from your data file, and split into an array, usi
+ng spaces as your deliminator
my @tmp = split(/\s+/,$line);
# set the i(th) row to the array from your file
$ret[$i] = \@tmp;
# increment the row number
$i++;
}
# return your array of arrays (i.e. a 2-d array)
return(\@ret);
}
__DATA__
11 12 13 14 15 16 17
21 22 23 24 25 26 27
31 32 33 34 35 36 37
41 42 43 44 45 46 47
51 52 53 54 55 56 57
61 62 63 64 65 66 67
-
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.