http://www.perlmonks.org?node_id=993748


in reply to sumof - attempting to sum a column from each file

$sumof needs to be outside of the while loop otherwise it will keep getting reset to zero and you should be using strict and warnings or even better in my opinion a v string (v5.14). Use local variables.. that is my $x; my @z;

hope this helps, - Rudolf

use v5.14; my @allfiles = <C:/Users/user/Desktop/DOwork/filez/nabillingscript/09_ +14_2012/nas/*>; for my $file (@allfiles) { open (FILE ,'<', $file) or die $!; my $sumof = 0; while(<FILE>){ my $lines = $_; my @kbused = split(/\s+/,$lines); push(my @kilo, $kbused[2]); for my $aline (@kilo){ $aline =~ s/\D//g; push (my @onlynums, $aline); $sumof += $_ for @onlynums; } } print "$sumof\n"; } close FILE;

Replies are listed 'Best First'.
Re^2: sumof - attempting to sum a column from each file
by ricky5ive (Initiate) on Sep 14, 2012 at 23:29 UTC
    This code you posted works perfectly. Appreciate you taking the time!

      No problem at all, you should consider learning more Perl if you have the time - it is a valuable investment. Not only a powerful tool but a fun artistic language! good luck

        I would like to learn more cause I do enjoy it, but hard to figure out where to go next? Any help there would be appreciated. I enjoy Perl, I just run into issues like these and it takes the wind out of my sails. I come back to it, but takes a few days. I've taken a beginners class and read the learning perl oriely book. I have the cookbook also, but it's difficult to figure out where to go with that. I've worked a lot with modules too. I think I need to get a more concrete understanding of the low level language so I can utilize more features and make it easier.

        Any suggestions are appreciated. If anyone knows what another stepping stone would be, I would be grateful.