CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
Hash key value is not returning properly

 



liton79
Novice

Sep 25, 2013, 8:22 AM

Post #1 of 4 (948 views)
Hash key value is not returning properly Can't Post

Hi Guru's,

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.



1 #!/usr/bin/perl
2 use warnings;
3 use strict;
4
5 my %hash;
6 my $key = "";
7 open (INFO, "month.txt") or die "couldn't open the input file $!";
8 while (<INFO>)
9 {
10 chomp;
11 if (/^December/)
12 {
13 push @{$hash{$key}}, $1;
14 print "$hash{$key} \n"
15 }
16 }
17 close (INFO);


OUTPUT:

==> ./readfile.pl
ARRAY(0x5063d0)


BillKSmith
Veteran

Sep 25, 2013, 9:45 AM

Post #2 of 4 (941 views)
Re: [liton79] Hash key value is not returning properly [In reply to] Can't Post

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


Laurent_R
Veteran / Moderator

Sep 25, 2013, 12:02 PM

Post #3 of 4 (939 views)
Re: [BillKSmith] Hash key value is not returning properly [In reply to] Can't Post

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.


liton79
Novice

Sep 26, 2013, 6:03 AM

Post #4 of 4 (927 views)
Re: [BillKSmith] Hash key value is not returning properly [In reply to] Can't Post

Thank you BilkSmith for your help. I was able to fix the problem.

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives