buggy
09-29-2004, 07:16 AM
Here is a function (GetCount)I have to count the number of occurences of a set of numbers in an file. The array comes from a file. I need to do this for 2 different files and I have put the whole piece in a foreach loop as I need to do it for 2 files. The problem is that my totals and information is duplicating. By that I mean all the figures and data for the first file is correct but the second is coming out as the second combined with the first.
Please note if I do it for either on its own all is fine, I cant see where the summing is happening
Any help would be great.
@files =('C:\Perl\one.txt','C:\Perl\two.txt');
foreach $file (@files) {
#read in data from file
open(fileIN,$file) or dienice("Cannot open $file: $!");
@FileData = <fileIN>;
close(fileIN);
print "GETTING VALUES FROM OUTPUT FILE\n";
foreach $line (@FileData) {
@line = split(':',$line);
push(@FinalOutput,$line[1]);
}
#get the unique elements.
GetCount(@FinalOutput);
}
}
sub GetCount{
@subject = @_;
$total = 0;
$value = 0;
$word = 0;
my %wordlist = ();
open(FileOut,">> Stats $folder .xls") or die ("Couldn't open Stats $folder for writing: $!");
flock(FileOut,2);
seek(FileOut,0,2);
foreach $word (@subject)
{
print FileOut $word,"\n";
$wordlist{$word}++;
}
while (($key, $value) = each(%wordlist))
{
if($key =~ /\w+/){
#print FileOut "Bracket : ", $key,"\n";
#print FileOut "Number of Jobs or Salaries : ", $value,"\n";
#print FileOut "\n";
$total = $total + $value;
}
}
print FileOut "Total : ", $total, "\n";
close(FileOut);
@subject='';
pop(@subject);
}
Please note if I do it for either on its own all is fine, I cant see where the summing is happening
Any help would be great.
@files =('C:\Perl\one.txt','C:\Perl\two.txt');
foreach $file (@files) {
#read in data from file
open(fileIN,$file) or dienice("Cannot open $file: $!");
@FileData = <fileIN>;
close(fileIN);
print "GETTING VALUES FROM OUTPUT FILE\n";
foreach $line (@FileData) {
@line = split(':',$line);
push(@FinalOutput,$line[1]);
}
#get the unique elements.
GetCount(@FinalOutput);
}
}
sub GetCount{
@subject = @_;
$total = 0;
$value = 0;
$word = 0;
my %wordlist = ();
open(FileOut,">> Stats $folder .xls") or die ("Couldn't open Stats $folder for writing: $!");
flock(FileOut,2);
seek(FileOut,0,2);
foreach $word (@subject)
{
print FileOut $word,"\n";
$wordlist{$word}++;
}
while (($key, $value) = each(%wordlist))
{
if($key =~ /\w+/){
#print FileOut "Bracket : ", $key,"\n";
#print FileOut "Number of Jobs or Salaries : ", $value,"\n";
#print FileOut "\n";
$total = $total + $value;
}
}
print FileOut "Total : ", $total, "\n";
close(FileOut);
@subject='';
pop(@subject);
}