I am new in Perl language and trying to write a simple perl program using hash table which will read a text file 'month.txt'. The program will search for a word (example: December) and should return that particular line that matches the word. If the line is repeated in the file numerous time it should return all the matching lines.

Below is my program and but I am not able to figured out why it is only returning the output 'ARRAY(0x5063d0)' instead of the lines that contains word December. Please help.

Also, would appreciate if you could explain to me the code on your reply which will help me to understand the code. Thank you in advance.

Congratulations for using strict and warning. I am surprised that they did not offfer any help this time.

The error causing the strange output is in the print statement. Your data structure is called a hash-of-arrays (refer: perldoc perldsc). In your case, that consists of a hash with one key and one corresponding value. That value is a reference to an array. Your output is what perl does when you try to print a reference rather than the thing it refers to.

Note: You may not be familiar with perl's documentation and its tool perldoc. At your command line, type

Code

perldoc perldoc

to learn to use the tool. Read the document I referred to (especially the section on hash-of-arrays). You will probably need to read some of the documents that it refers to.

The rules for dereferencing are a bit complicated. I will give you this answer.

Code

print "@{$hash{$key}} \n"

With this fixed, you will get error messages which should help solve your other problems yourself. Do not hesitate to ask more questions. Just show us what you have tried. Good Luck, Bill

Well there is another major problem in your script: the $key variable is never set to anything useful. And the bad news is that it probably indicates that you don't really understand what you are doing in your code.

Please explain exactly what you are trying to do and what you wish to obtain.