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

Greetings,

I have 2 flat files (both in similar format) as follows :
012 00003200Thanksgiving road Thanksgiving Town TT022 A + B S
I am reading this into 2 hashs : %File2 and %File3 and the key of hash is "00003200".
From the 3rd flat file, I have the same value "00003200" and for this value, I need to go and look into %File2 and pick up all the values associated with the key.

Question:

When I do the following :
@line = parse_line($ln) # this will parse the line using substr and sa +ve each value in array. if (exists $File2{$line[3]} ) { print "Found the match ",Dumper($File2{$line[3]}),"\n"; } else { print "Could not find the match \n"; } # Tried the following and it does not work either : if (exists $File2{"$line[3]"} ) { print "Found the match ",Dumper($File2{"$line[3]"}),"\n"; } else { print "Could not find the match \n"; }
The above statement always fails. When I do a simple print before if statement by giving actual value of "00003200" , I do see values.

What am I missing?

Thanks for help in advance!!

Replies are listed 'Best First'.
Re: hash question..
by toolic (Bishop) on Nov 22, 2007 at 15:54 UTC
    As a debug step try printing the contents of all your variables:
    print "line contains:\n"; print Dumper(\@line); print "File2 contains:\n"; print Dumper(\%File2); print "File3 contains:\n"; print Dumper(\%File3);

    I believe "$line[3]" is the same as $line[3]. So, you should expect them to behave the same.

      I did do that, and yet I do not see same behaviour.
      When I do
      print Dumper{$File2{"00003200"});

      I do get the value for that key!.

      What am I doing wrong here?

        Please add these 2 lines to your script, then run your script, then post the output of your script so that we may all see the contents of the @line array:
        print "line contains:\n"; print Dumper(\@line);

        It will be useful for the rest of to know if this is a simple array, or an array of arrays, or an array of hashes, etc. And we will be able to see if there is even a 4th element of the array.

        If the File hashes are not too big, please also post their contents.

        Also, make sure you are using the strictures (use warnings; use strict;), as these may help to point out undefined or unititialized variables.