snape has asked for the wisdom of the Perl Monks concerning the following question:
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.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Hash of hashes: Check if the element is present in previous hash table
by graff (Chancellor) on Apr 04, 2012 at 03:08 UTC | |
|
Re: Hash of hashes: Check if the element is present in previous hash table
by Riales (Hermit) on Apr 03, 2012 at 23:45 UTC | |
by snape (Pilgrim) on Apr 03, 2012 at 23:59 UTC |