Help with Hash


D

Deepu

Hi All,

If i have a files located in different directories with contents like:

File_A:

ABCD : 1
DECF : 0
DREF : 1
DSER : 0
QWSA : 0

File_B:

ABCD : 1
DECF : 1
DREF : 0
DSER : 0
QWSA : 0

File_C:

ABCD : 1
DECF : 0
DREF : 1
DSER : 0
QWSA : 1

How can i generate a output file:

File_Output:

ABCD : 1
DECF : 1
DREF : 1
DSER : 0
QWSA : 1

In addition to this FileOutput should contain:

Num of Files: 3
Num of Elements: 5
Num of Hits : 4 (Elements with 1 after comparing all 3 files)


I have the code like below but would like to know how i get the Num of
Hits after comparing all 3 files.

use strict;
use Getopt::Long;
use IO::Handle;

use vars qw($opt_help $opt_dir $opt_debug);

&GetOptions("help|h" => \$opt_help,
"dir|d=s" => \$opt_dir,
"debug" => \$opt_debug
);


my $Dir = "$opt_dir";

my $num_of_files = 0;
my $num_of_elements = 0;
my $num_of_hits = 0;

my %num_of_elements = ();

opendir (DIR, $Dir) || die "Can't open $Dir - $!\n";

foreach my $tempdir (readdir DIR)
{
if (-d "$Dir/$tempdir")
{
$num_of_files = $num_of_files + 1;

opendir (SUB, "$Dir"."/$tempdir" ) || die "Can't open
$tempdir - $!\n";

my $Name = $tempdir;

my $elementFile = "$Dir/$tempdir/"."File_$Name";

open (FH, "$elementFile") || die "Can't open $elementFile -$!
\n";

while (<FH>)
{
if (/(\S+)\s+\:\s+(\d+)/)
{
$num_of_elements{$1}++;
}
}
close (FH);

closedir (SUB);
}
}

closedir (DIR);

foreach my $element (keys %num_of_elements)
{
$num_of_elements = $num_of_elements + 1;
}

print ("\n________________________________________\n");
print ("Num of Files: $num_of_files \n");
print ("Num of Elements: $num_of_elements \n");
print ("Num of Hits: $num_of_hits \n");
print ("________________________________________\n");
 
Ad

Advertisements

D

Deepu

    You should always prefer lexical variables over package variables,
    except when you can't.

Why do you think you need package variables here?

You don't, so you should use lexical variables instead:

    my($opt_help $opt_dir $opt_debug);

Thanks for this info..i will use the lexical variables as there was no
reason for using package variables...

So can you suggest a logic to get the $num_of_hits for the
$num_of_elements after comparing all 3 files?
 
Ad

Advertisements

D

Deepu

my %out;
foreach my $file_contents ($file_a, $file_b, $file_c) {
    foreach my $file_line (split /\n/, $file_contents) {
        my($string, $count) = split / : /, $file_line;
        if ($count) {
            $out{$string} = 1;
        }
        else {
            $out{$string} = 0 unless exists $out{$string};
        }
    }

}

foreach my $string (sort keys %out) {
    print "$string : $out{$string}\n";

}

my $hits_count = grep $_, values %out;
print "Num of Hits : $hits_count\n";
------------------------

Thanks Tad McClellan!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top