I have data sturcture that looks as follows in about 15-20 diff files.
A 1 1
B 0 1
C 0 2
D 1 2
E 0 1
F 0 2
I'm trying to open each file and for each val in Col3, count the total # of 0 or 1 in col2.
For 1 count for 1 = 1
For 2 count for 1 = 1
Total = 2
For 1 count for 0 = 2
For 2 count for 0 = 2
Total = 4
It works fine when I do for a SINGLE file, but when I try to do for multiple files, it keeps on adding the values foreach file. I know I'm doing something crazy here, but can't point it.
Should I add a counter and set it 0 for each iteration?
A 1 1
B 0 1
C 0 2
D 1 2
E 0 1
F 0 2
I'm trying to open each file and for each val in Col3, count the total # of 0 or 1 in col2.
Code:
opendir DIR,"$path" or die "Cannot readdir $!\n";
@files = grep (/^\w+.act.*.txt$/,readdir DIR);
closedir DIR;
foreach (sort @files) {
@fData = "cat $path/$_ |";
while (@fData) {
($c1,$c2,$c3) = split (/\s+/,@fData);
$actKey{$c3}++ if ($c2 == 0);
$act++ if ($c2 == 0);
$preKey{$c3}++ if ($c2 == 1);
$preC++ if ($c2 == 1);
}
foreach (sort keys %actKey) {
print "For $_ count = $actKey{$_}\n";
}
print "Total = $act\n\n";
foreach (sort keys %preKey) {
print "For $_ count = $preKey{$_}\n";
}
print "Total = $preC\n\n";
}
For 1 count for 1 = 1
For 2 count for 1 = 1
Total = 2
For 1 count for 0 = 2
For 2 count for 0 = 2
Total = 4
It works fine when I do for a SINGLE file, but when I try to do for multiple files, it keeps on adding the values foreach file. I know I'm doing something crazy here, but can't point it.
Should I add a counter and set it 0 for each iteration?