Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Re: read files into 2D arrays and print element?

by fasoli (Beadle)
on Nov 30, 2016 at 17:45 UTC ( [id://1176952]=note: print w/replies, xml ) Need Help??


in reply to read files into 2D arrays and print element?

Hi again all. Thanks to your help I solved my first problem, thank you again. Now what I'm trying to do is get the average value of my four matrices' elements (to then proceed to getting the standard deviation). They are four 3x3 test matrices, each one in a different file. They are test cases as my real matrices are thousands of lines long and the number of files is again thousands.

When I print them with  $line[$a][$b] they look correct, so I've commented this line out as it was used as a test to see if they look ok. The output I'm getting if I try to get the sum of the elements and then the mean, is  Use of uninitialized value in addition (+) at test_SD_Wednesday.pl line 52 and a bunch of wrong numbers as sum/averages. I'm pretty sure I'm screwing something up badly but I can't figure out what. Any hints please? I'm starting to get scared my supervisors will really shout at me or think I'm an idiot if I tell them it's taken me 10 days to have a script that doesn't work...

Finally can someone comment on the indentation? Does it make sense they way I did it? It looks less messy but no clue if I got the philosophy behind it.

#/bin/perl/ use strict; use warnings; use autodie; my $molec = "1ac6"; my $cluster; my $times; my $input; my $path = "/media/RAIDstorage/home/athina/dist-analysis/${molec}/time +series/test"; my $line; my @files; @files = `ls $path\/$molec-times*`; foreach (@files) { my $a; my $b; my $m_avrg; my @m_avrg; undef @m_avrg; my @list; my $list; my $aver; /${molec}-times-(\d+)-clust(\d{1})/; $times = $1; $cluster = $2; open $input, '<', "$path\/${molec}-times-${times}-clust${cluster}.out +" or die $!; while ($line = <$input>) { chomp $line; push @list, [split/\s+/, $line]; } # while input close $input; for ($a=0; $a<=2; $a++) { for ($b=0; $b<=2; $b++) { #print "$list[$a][$b] "; # check matrices $m_avrg[$a][$b] = ($m_avrg[$a][$b] + $list[$a][$b]); print "$m_avrg[$a][$b] \n"; } print " \n"; } print "average\n"; for ($a=0; $a<=2; $a++) { for ($b=0; $b<=2; $b++) { $m_avrg[$a][$b] = $m_avrg[$a][$b] / 4; print "$m_avrg[$a][$b] "; } print "\n"; } } # foreach file in loop

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (4)
As of 2024-04-25 07:58 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found