Hi, Monk

I have 4 related files and my main objective is to look for common elements in all the four files. I would like to use Hash of hashes here and then include the common elements in other common hash table. For example:

Elements in File1: 1. Hi 2. March 3. Aug 4. Sept 5. Oct Elements in File2: 1. March 2. Hi 3. Bye 4. Aug 5. Dec Elements in File3: 1. Hi 2. March 3. Aug 4. Sept 5. Oct Elements in File4: 1. Hi 2. March 3. Aug 4. Bye 5. Dec

Here is my code

my hoh; my hash; for(my $i = 0; $i <= 3; $i++){ my $condition = "first" if ($i == 0); $condition = "second" if ($i == 1); $condition = "third" if ($i == 2); $condition = "forth" if ($i == 3); ## Reading the file open my $IN, $i.".txt or die $!; while(<$IN>){ chomp($_); my $ele = $_; $hoh{$i}{$_} = 1; ### Look for that element if it exists in $hoh{$i}{$_} if $i >=1 for a +ll $i ## for the first file if (!exists $hash{$_}){ $hash{$_} = 10**$i; } elsif ($i >= 1){ #### code to check the common element in all $i #### if the common element is present then my val = $hash{$_} my newVal = int($val) + 10**$i; $hash{$_} = newVal; } } close($IN); }

I am trying to look for common elements among all the files. Say, if an element is present in all the three file and forth then it should not be counted among the common elements. Therefore my result %hash should have only March, Hi and Aug.


In reply to Hash of hashes: Check if the element is present in previous hash table by snape

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.