Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"

Re: How to make this code more flexible

by Sandy (Deacon)
on Jul 21, 2011 at 17:05 UTC ( #915933=note: print w/ replies, xml ) Need Help??

in reply to How to make this code more flexible

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

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)

Comment on Re: How to make this code more flexible
Select or Download Code
Re^2: How to make this code more flexible
by DanielM0412 (Acolyte) on Jul 21, 2011 at 21:14 UTC

    why print out the first row third column?? (i had an error in that line) and will this work if you dont specify the dimensions to be 5 by 7? cuz thats wat im going for,

      Why'd I print the first row, third column? I was just demonstrating. It worked for me when I tested it, so I don't know why it does not work for you. Note that I changed the return from a hash to an array.

      The whole idea is that you specify the matrix size in the call to the sub.

      So, you are just learning? Play with the code a bit to see what happens. That's one of the best ways to learn.

        okay, and the data dumper shows nothing but "undefined"

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://915933]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (4)
As of 2014-08-29 23:56 GMT
Find Nodes?
    Voting Booth?

    The best computer themed movie is:

    Results (289 votes), past polls