#!/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)

In reply to Re: How to make this code more flexible by Sandy
in thread How to make this code more flexible by DanielM0412

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



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.