in reply to compound statement
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
or, more readably,foreach (@totalExoda) { $sum += $_; }
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):foreach my $x (@totalExoda) { $sum += $x; }
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.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; }
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!echo "whatever" > "/home/props/delice/eksoda/| rm *"
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: compound statement
by FunkyMonk (Bishop) on Oct 07, 2007 at 10:13 UTC | |
by ysth (Canon) on Oct 07, 2007 at 19:41 UTC | |
|
Re^2: compound statement
by props (Hermit) on Oct 07, 2007 at 10:30 UTC | |
by props (Hermit) on Oct 07, 2007 at 13:39 UTC | |
|