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.
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.