waytoperl has asked for the wisdom of the Perl Monks concerning the following question:

How to compare hash value exists in text file. If exists then print key & value exists. Hash table have identical values stored in hash Keys.

Text file contents:

line 1 have some string here and Hash_key_4: Hash_value_4_identical_to +_Hash_value_9 line 2 have some string here and Hash_key_5: Hash_value_5 line 3 have some string here and Hash_key_6: Hash_value_6 line 4 have some string here and Hash_key_7: Hash_value_7_identical_to +_Hash_value_2 line 5 have some string here and Hash_key_1: Hash_value_1 line 6 have some string here and Hash_key_2: Hash_value_2_identical_to +_Hash_value_7 line 7 have some string here and Hash_key_3: Hash_value_3 line 8 have some string here and Hash_key_8: Hash_value_8 line 9 have some string here and Hash_key_9: Hash_value_9_identical_to +_Hash_value_4 line 10 have some string here and Hash_key_10: Hash_value_10 .... ....

Hash table: Key are not identical. Few Values are identical.

Hash_key_1 => Hash_value_1 Hash_key_2 => Hash_value_2_identical_to_Hash_value_7 Hash_key_3 => Hash_value_3 Hash_key_4 => Hash_value_4_is_identical_to_Hash_value_9 Hash_key_5 => Hash_value_5 Hash_key_6 => Hash_value_6 Hash_key_7 => Hash_value_7_identical_to_Hash_value_2 Hash_key_8 => Hash_value_8 Hash_key_9 => Hash_value_9_is_identical_to_Hash_value_4 Hash_key_10 => Hash_value_10 .... ....

Please help

my %hash = (); my $filename = "input_text.txt"; open FILE,"<$filename" or die "Cannot read the file $filename: $!\n"; foreach <FILE> { if (my $line =~ m/{$Hash_value{key}}/) { print "$line has $hash{$key} and value is $hash{key}\n"; } }

Replies are listed 'Best First'.
Re: compare text file and print occurrence of key and value
by Laurent_R (Canon) on Sep 23, 2014 at 21:02 UTC
    This is a quick example of the process that I had in mind as described in my previous post:
    use strict; use warnings; use feature qw /say/; my %hash = (a => 1, b => 2, c => 3, d => 2, e => 5, f => 3); my %HoA; for my $key (keys %hash) { my $value = $hash{$key}; push @{$HoA{$value}}, $key; } for my $key2 (keys %HoA) { my @keys = @{$HoA{$key2}}; say "Keys @keys have the same value: $key2 "; }
    Which produces the following output:
    $ perl reverse_hash.pl Keys a have the same value: 1 Keys c f have the same value: 3 Keys b d have the same value: 2 Keys e have the same value: 5
    The formatting is not exactly what you wanted, but you have the basic algorithm and the results seem correct, I leave it to you to format the output according to your needs. Don't hesitate to ask if you have any further difficulty.

    Just in case you need it for better understanding, this is what the HoA looks like at the end of the first loop populating it:

    0 HASH(0x60025cbb8) 1 => ARRAY(0x60006f210) 0 'a' 2 => ARRAY(0x600462490) 0 'b' 1 'd' 3 => ARRAY(0x60048d1b8) 0 'c' 1 'f' 5 => ARRAY(0x600495808) 0 'e'

      From hash table need to generate required output stored in text file.

      Do i need to define "$key_x" in another hash? Is there another way to generate

      SOME-DATA.6227....

      SOME-DATA is identical, replace .1.3.5.7.9.1 with SOME-DATA

      for my $key ( keys %hash ) { delete $hash{$key}; $hash{$key} = "$key_x"; }
      __Content of hash__
      $VAR1 = { '.1.3.5.7.9.1.6227.1.20.1.1.2.33.0.7.7' => 'number: 2', '.1.3.5.7.9.1.6227.1.20.1.4.8.88.6.3' => 'number: 2', '.1.3.5.7.9.1.6227.1.20.1.5.3.5.2' => 'number: 3', '.1.3.5.7.9.1.6227.1.20.1.6.2.88.2.3.30.4' => 'number: 3', '.1.3.5.7.9.1.6227.1.20.1.1.7.88.5.3' => 'number: 3', '.1.3.5.7.9.1.6227.1.20.1.1.6.22.2.3.30' => 'data: ', '.1.3.5.7.9.1.6227.1.20.1.2.9.88.2.3.30.4' => 'number: 3', '.1.3.5.7.9.1.6227.1.20.1.5.8.88.2.3.30.4' => '', '.1.3.5.7.9.1.6227.1.20.1.6.4.88.2.3.30.4' => 'number: 1', '.1.3.5.7.9.1.6227.1.20.1.7.3.88.2.3.30.4' => 'number: 2', '.1.3.5.7.9.1.6227.1.20.1.8.2.88.2.3.30.4' => 'number: 3', '.1.3.5.7.9.1.6227.1.20.1.9.1.88.2.3.30.4' => 'number: 4', '.1.3.5.7.9.1.6227.1.20.1.9.2.88.2.3.30.4' => 'number: 5', '.1.3.5.7.9.1.6227.1.20.1.3.3.88.2.3.30' => 'number: 6', '.1.3.5.7.9.1.6227.1.20.1.6.9.0' => 'number: 7', '.1.3.5.7.9.1.6227.1.20.1.7.1.88.2.3.30.4' => 'data: ', '.1.3.5.7.9.1.6227.1.20.1.8.1.88.2.3.30.4' => 'number: 8' };
      __Required output__
      SOME-DATA.6227.1.20.1.1.2.33.0.7.7' = 'number: 2', SOME-DATA.6227.1.20.1.4.8.88.6.3' = 'number: 2', SOME-DATA.6227.1.20.1.5.3.5.2' = 'number: 3', SOME-DATA.6227.1.20.1.6.2.88.2.3.30.4' = 'number: 3', SOME-DATA.6227.1.20.1.1.7.88.5.3' = 'number: 3', SOME-DATA.6227.1.20.1.1.6.22.2.3.30' = 'data: ', SOME-DATA.6227.1.20.1.2.9.88.2.3.30.4' = 'number: 3', SOME-DATA.6227.1.20.1.5.8.88.2.3.30.4' = '', SOME-DATA.6227.1.20.1.6.4.88.2.3.30.4' = 'number: 1', SOME-DATA.6227.1.20.1.7.3.88.2.3.30.4' = 'number: 2', SOME-DATA.6227.1.20.1.8.2.88.2.3.30.4' = 'number: 3', SOME-DATA.6227.1.20.1.9.1.88.2.3.30.4' = 'number: 4', SOME-DATA.6227.1.20.1.9.2.88.2.3.30.4' = 'number: 5', SOME-DATA.6227.1.20.1.3.3.88.2.3.30' = 'number: 6', SOME-DATA.6227.1.20.1.6.9.0' = 'number: 7', SOME-DATA.6227.1.20.1.7.1.88.2.3.30.4' = 'data: ', SOME-DATA.6227.1.20.1.8.1.88.2.3.30.4' = 'number: 8'
        You could just do this:
        my $substit = quotemeta (".1.3.5.7.9.1") for my $key (keys %hash) { my $value = $hash{$key); $key =~ s/^$substit/SOME_DATA/; print "$key = $value\n" }
Re: compare text file and print occurrence of key and value
by Laurent_R (Canon) on Sep 23, 2014 at 20:44 UTC
    It is a bit difficult to understand what you want to do. But, assuming I nonetheless understood, one solution is to reverse the hash: read all you hash values, use the values as keys, and store the keys as values. But because hash can't have duplicate keys, you'll need either to push the original keys onto an sub-array (using a hash of arrays), or to concatenate the keys as strings in the new hash. At the end of the process, you go through the new hash.
Re: compare text file and print occurrence of key and value
by AppleFritter (Vicar) on Sep 23, 2014 at 20:31 UTC
    Oops, misread your question, sorry.