#!/usr/bin/perl
use warnings;
use strict;
use Data::Dumper;
my $filepath = "OP.txt";
my $olddata = readdt2($filepath,5,7); # 5x7 array
print Dumper($olddata);
# print the 1st row, 3rd column
my $i = 1;
my $j = 3;
print "1st row, 3rd column: ",$olddata->[$i-1][$j-1],"\n";
sub readdt2 {
my $ifn = shift;
my $I = shift;
my $J = shift;
# open(my $IFH, "<$ifn") or die "cannot open file $ifn\n";
my $line;
my @ret;
my @tmp;
for my $j (0 .. $I-1) {
$line = <DATA>;
chomp($line);
@tmp = split(/\s+/,$line);
push @{$ret[$j]},@tmp[0..$J-1];
}
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
Notes:
push @{$ret[$j]}, @tmp[0..$J-1];
1. This line will automatically make
$ret[$j] an array ref.
2.
@array[1,2,3] returns a list of the specified elements.
3.
[1..3] returns a list of (1,2,3)