in reply to Read file in a hash and compare the 'values' from file 2

So now I have got keys and values.

You may think you do, but you don't. Post runnable code ala How do I post a question effectively?, like

#!/usr/bin/perl -- #~ 2011-07-05-05:39:58 Anonymous Monk #~ http://perlmonks.com/?node_id=912812# Read file in a hash and compa +re the 'values' from file 2 #~ perltidy -csc -otr -opr -ce -nibc -i=4 use strict; use warnings; use autodie; use Data::Dumper qw/ Dumper /; use 5.010; Main(@ARGV); exit(0); sub Main { if ( @_ == 2 ) { NotDemoMeaningfulName(@_); } else { Boobbly(); print Usage(); } } ## end sub Main sub NotDemoMeaningfulName { my ( $inputFile, $outputFile ) = @_; open my ($inFh), '<', $inputFile; open my ($outFh), '>', $outputFile; my $d; my $read; my %hash; while (<$inFh>) { $d = $_; $read .= $d; my ( $key, $val ) = split /\n/, $read; $hash{$key} = $val; } ## end while (<$inFh>) print $outFh Dumper( \%hash ); } ## end sub NotDemoMeaningfulName sub Usage { <<"__USAGE__"; $0 inputfile outputfile __USAGE__ } ## end sub Usage sub Boobbly { #~ http://perlmonks.com/?abspart=1;displaytype=displaycode;node_id=912 +812;part=1 my $Furrry = <<'__Furrry__'; >1 ##key abcdef ##value >2 hijkl >3 abnikhl..etc __Furrry__ NotDemoMeaningfulName( \$Furrry, \my $Farts ); print $Farts, "\n\n"; } ## end sub Boobbly __END__ $VAR1 = { '>1 ##key' => 'abcdef ##value' };

Also read these FAQ items
How can I tell whether a certain element is contained in a list or array?
How do I compute the difference of two arrays? How do I compute the intersection of two arrays?
How do I test whether two arrays or hashes are equal?

Replies are listed 'Best First'.
Re^2: Read file in a hash and compare the 'values' from file 2
by Anonymous Monk on Jul 05, 2011 at 13:14 UTC
    Thanks for the code, Now I got my keys and values from file 1 in $outputFile. what I need now is to open File2:
    #file2 abcdef hijkl mnop qrstuv wxyzabc
    compare it one by one with the 'values' of $outputFile and print the key of $outputFile corresponding to the values matched in file2. so that the output wud be like:
    >1 abcdef >4 xyzabc . . etc

      Thanks for the code, Now I got my keys and va... what I need now is to open ...

      My code merely demonstrates how your code fragment doesn't work. The hash only contains one key value pair.

      Its an example of runnable code.

      You claim %hash is correctly populated? Then you need to show that code, and make sure it is runnable in a similar fashion as I did.

      If you want to start with my code, you need to edit the program until %hash is correctly populated, while at the same time changing the function/variable names to be meaningful. Use Data::Dumper at various points to see where your assumptions don't match, its part of the Basic debugging checklist.

      $outputFile doesn't make sense as a variable name for a file that you wish to read from.

      Similarly NotDemoMeaningfulName and Boobly aren't great function names.

      See Is PerlMonks a good place to get answers for homework?