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

Hi monks, I have a trouble in reading a text file in a hash variable. My file is like:
>1 ##key abcdef ##value >2 hijkl >3 abnikhl..etc
for that I wrote the code like:
while(<FILE>) { $d=$_; $read.=$d; my ($key, $val) = split /\n/, $read; $hash{$key}= $val;
So now I have got keys and values. What I need now is to compare each 'value' of file 1 with each value of file2 and if the values are same, assign the 'key' of file1 to the value of file2. file 2 format is:
abcde hijkl mnop qrst uvwx..etc

Replies are listed 'Best First'.
Re: Read file in a hash and compare the 'values' from file 2
by CountZero (Bishop) on Jul 05, 2011 at 15:13 UTC
    Did you check what is in your %hash? I think you will find it does not contain the data as you expect.

    CountZero

    A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

Re: Read file in a hash and compare the 'values' from file 2
by jethro (Monsignor) on Jul 05, 2011 at 15:34 UTC

    A hash is good at getting the value from a key. A hash is not good at getting the key from a value which is what you propose to do.

    If all your keys are consequtive numbers you might as well use an array. Even better would be using a hash with key and values switched, i.e. your key is the value and your value is the key. That way, you can just check if your values in file2 exist as keys in the hash (which is the exactly the operation hashes are built for)

Re: Read file in a hash and compare the 'values' from file 2
by Anonymous Monk on Jul 05, 2011 at 12:44 UTC

    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?
      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?