Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??
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

In reply to Re^7: 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!
  • 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, 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, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            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.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others chilling in the Monastery: (7)
    As of 2014-10-21 08:21 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      For retirement, I am banking on:










      Results (98 votes), past polls