Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to clear an array for 2nd iteration. 2

Status
Not open for further replies.

max1x

Programmer
Jan 12, 2005
366
US
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.

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?
 
try:

my @fData = "cat $path/$_ |";

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
With this line, you're just creating an array with one element, namely the string that you specify.
Code:
 @fData = "cat $path/$_ |";
If you want to execute the 'cat' command, you need to use backticks rather than double quotes.
 
Kevin, ishnid,

Thank you. I've tried both, but it still does the same.
 
The problem is the scope of your counter variables and hashes.
(In addition to you using double-quotes instead of backticks).

Code:
    ...

    @fData = `cat $path/$_ |`;

    [COLOR=red][b]my(%actKey,%preKey,$act,$preC) = ((),(),0,0);[/b][/color]
 
    while (@fData) {
        ($c1,$c2,$c3) = split (/\s+/,@fData);

    ...

This way, your hashes and counters get reset for each file.
 
You need both... at the same time.. backticks around your command and the "my" that Kevin specified
 
Thank you every one for your help :),this was the missing piece.

Code:
my(%actKey,%preKey,$act,$preC) = ((),());

I did tried this earlier, but it did not work;
my %actKey, %perKey;
 
Whilst the 'my @fData' won't hurt, I don't believe it addresses the OP's problem: Not resetting his per-file counters for each file.

The assignment of the output of the backticked command to @fData replaces the array anyway rather than appends to it.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top