There are a couple of different problems.
First, what is the problem to be solved? Is the subroutine supposed to print running total after each file is processed? And should the printed value be the total for just that file or for all the files that have been processed? Should the subroutine return a value?
Next, my @totalExoda = 0; isn't written correctly, it could be written as @totalExoda = (0); to initialize the array to contain a single zero, but what you probably meant was @totalExoda = (); to initialize it with zero elements.
When you add all the elements of @totalExoda, you need to say either
foreach (@totalExoda) {
$sum += $_;
}
or, more readably,
foreach my $x (@totalExoda) {
$sum += $x;
}
However, unless there is some reason that you need the array @totalExoda, it would be better to just sum as you go, something like this. (I am assuming you want to return $sum rather that printing it once per file, and this code is not tested):
sub Esoda_Statistika_button {
my $sum = 0;
chdir "/home/props/delice/eksoda" or die "didn't make to eksoda f
+older\n";
foreach my $file (glob '*.txt') {
open my $in, "<", $file;
while (<$in>) {
$sum += $1 if /Exoda.*Total:(.*\d)/;
}
}
return $sum;
}
Notice that I also changed the two-argument form of open to the three argument form. This is to avoid possible disaster. Consider what would happen if someone created a very badly named file in the eskoda directory, for example with the following command to the shell.
echo "whatever" > "/home/props/delice/eksoda/| rm *"
The two argument form of open (without a "<" in front of the filename) will merrily delete all of your files when you run your script!
|