Hello all - I'm fairly new to Perl, I've been learning for the last year. I'm getting confused by this script I've been working on and was looking to see if someone can help me. I want to sum every file. I wrote this script originally using one specific file and it worked flawless, but now I want it to do this with each file in a specific directory and return the sum for each file for the specific column.
First script that only uses one file:
open (FILE ,"<", "Myfile");
while($lines=<FILE>)
{
###-reads each line from the file and splits it by multiple spaces
@kbused = split(/\s+/,$lines);
push(@kilo, $kbused[2]);
}
####-takes the number from column2 and removes the KB from the end
foreach $aline (@kilo)
{
$aline =~ s/\D//g;
push (@onlynums, $aline);
}
$sumof += $_ for @onlynums;
print "$sumof\n";
close FILE;
The script I'm working on now that will do it for each file in the directory:
@allfiles = <C:/Users/user/Desktop/DOwork/filez/nabillingscript/09_14_
+2012/nas/*>;
foreach $file (@allfiles)
{
open (FILE ,"<", "$file");
while($lines=<FILE>)
{
my $sumof = 0;
@kbused = split(/\s+/,$lines);
push(@kilo, $kbused[2]);
foreach $aline (@kilo)
{
$aline =~ s/\D//g;
push (@onlynums, $aline);
$sumof += $_ for @onlynums;
}
}
print "$sumof\n";
}
close FILE;
I can't figure this out on my own for some reason, but I can't learn without asking for help.
Thank you.
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.