Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

How to make this code more flexible

by DanielM0412 (Acolyte)
on Jul 21, 2011 at 16:26 UTC ( [id://915921]=perlquestion: print w/replies, xml ) Need Help??

DanielM0412 has asked for the wisdom of the Perl Monks concerning the following question:

Sorry if this seems like a stupid question, youll be gettin a lot of these from me :) , as im 14 and on an internship, but anyways i wrote a function to read in a 4by4 matrix, and split it (for future use), but my question is how do i do the alter my code to make it able to read in a matrix with unknown dimensions H and K, heres my very limited code below

#!/usr/bin/perl use warnings; use strict; use Data::Dumper; my $filepath = "OP.txt"; my $olddata = readdt2($filepath); print Dumper($olddata); sub readdt2 { my $ifn = shift; open(my $IFH, "<$ifn") or die "cannot open file $ifn\n"; my $line; my @nt = ("A","C","G","T"); my %ret; my @tmp; for my $j(@nt) { $ret{$j} = []; $line = <$IFH>; chomp($line); @tmp = split(/\s+/,$line); for (my $i=0; $i<=$#tmp; $i++ ) { $ret{$j}[$i]= $tmp[$i] ; } } close($IFH); return(\%ret); }

All advice much appreciated, thanks for your time and patience

Replies are listed 'Best First'.
Re: How to make this code more flexible
by Sandy (Curate) on Jul 21, 2011 at 17:05 UTC
    #!/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)

      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.

Re: How to make this code more flexible
by Marshall (Canon) on Jul 21, 2011 at 22:44 UTC
    You are doing GREAT for just starting!
    First, multi-dimensional data structure consists of only references until you get to the last dimension. An ArrayOfArray is what the name says, an array of references to arrays. Earlier I think I saw a 2D structure that you built with a hash for the first dimension. That is not the usual case as you don't get easy sequential processing with a hash. But sometimes this is useful. In that case this is a HashOfArray, or hash of references to arrays.

    Below is some code that makes both types and returns a reference to each one. There are no limits on the size of the AoA or HoA. I do a split to get @row from the data line. The default split is on 'space',\t\f\r\t so this also gets rid of the end of line (no need for chomp). Then I just push a reference to this row array onto the AoA and to the HoA. One line of code for each one.

    Whenever your code encounters a "my" variable statement. You are guaranteed to get a "new one". Perl will reuse the space the last one used or if it can't because a reference to it is in existence, new memory will be allocated.

    #!/usr/bin/perl -w use strict; use Data::Dump qw(pp); my ($arrayRef, $hashRef) = makeBothTypes(); sub makeBothTypes { my $rowName = 'A'; my @twoDarray; my %twoDhash; while (<DATA>) { my @row = split; # no chomp needed for default split push @twoDarray, \@row; $twoDhash{$rowName++} = \@row; } return (\@twoDarray,\%twoDhash); } print pp($arrayRef); print pp($hashRef); print "\n"; print "$hashRef->{B}[2]\n"; #23 print "$arrayRef->[1][2]\n"; #23 =output that this prints.... [ [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], ]{ A => [11, 12, 13, 14, 15, 16, 17], B => [21, 22, 23, 24, 25, 26, 27], C => [31, 32, 33, 34, 35, 36, 37], D => [41, 42, 43, 44, 45, 46, 47], E => [51, 52, 53, 54, 55, 56, 57], } 23 23 =cut __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

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://915921]
Approved by davido
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (3)
As of 2024-03-29 07:28 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found