in reply to compound statement

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!

Replies are listed 'Best First'.
Re^2: compound statement
by FunkyMonk (Bishop) on Oct 07, 2007 at 10:13 UTC
    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!
    No it won't. open, with two arguments, defaults to opening for reading.

    update: As ysth pointed out this quote was referring to a specific use of open. Thanks ysth, and my apologies to quester. Next time I'll read your nodes more thoroughly.

      Read the example above that statement more closely.
Re^2: compound statement
by props (Hermit) on Oct 07, 2007 at 10:30 UTC
    I really appreciate your help on this this is a sample txt file with content:
    Supplier1 1 Supplier2 2 Supplier3 3 Exoda Total:6 euros
    i need to get from every input file that Total:6 euros ( The number 6 in this case). The logic behind it is that i need to add my daily shop expenses in a file everyday. So whenever i want to check my monthly or yearly expenses to run this subroutine and get the total expenses. So i need one number but i do something wrong i get the following output:
    0 0 0 0 0 0 0 0 0 6 6 12 12 18 18 24 24 30 30 36 36 42 42 48 48 54 54 60 66 66 72 78 78 84 90 90 96 102 102 108 114 114 120 126 126 132 138 138 144 150 150 156 162 162 168 174 274
    there is a confusion somewhere
      well this code works for me
      sub Esoda_Statistika_button { my @totalExoda = (); my $sum = 0; my $clearedValue=0; chdir "/home/props/delice/eksoda" or die "didn't make to eksoda folde +r\n"; foreach my $file (glob '*.txt') { open IN, $file; while (<IN>) { if ($_ =~ m/(Exoda.*Total:)(.*\d)/) { $clearedValue =$2; push(@totalExoda,$clearedValue); } #if } #end while } #end outer foreach foreach (@totalExoda){ $sum+= $_; } #end inner foreach chomp $sum; print "$sum\n"; } #end sub
      this thing stresses my head :)
    A reply falls below the community's threshold of quality. You may see it by logging in.