Help with Hash

Discussion in 'Perl Misc' started by Deepu, Oct 14, 2010.

  1. Deepu

    Deepu Guest

    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");
     
    Deepu, Oct 14, 2010
    #1
    1. Advertising

  2. Deepu

    Deepu Guest

    >     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?
     
    Deepu, Oct 14, 2010
    #2
    1. Advertising

  3. Deepu

    Deepu Guest

    > 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!
     
    Deepu, Oct 14, 2010
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Red Orchid
    Replies:
    3
    Views:
    1,047
  2. Pieter Claassen
    Replies:
    1
    Views:
    1,114
    CBFalconer
    Aug 4, 2004
  3. Bo Peng
    Replies:
    4
    Views:
    790
  4. rp
    Replies:
    1
    Views:
    534
    red floyd
    Nov 10, 2011
  5. Srijayanth Sridhar
    Replies:
    19
    Views:
    624
    David A. Black
    Jul 2, 2008
Loading...

Share This Page