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: Intermediate:
Hash tables

 



aceall
Novice

Mar 10, 2010, 8:59 AM

Post #1 of 7 (1103 views)
Hash tables Can't Post

Folks,

I need to know the best way to create hash tables from a flat file.

For Eg:
Suppose my flat file contains the following

abc,abc-1
abc,abc-2
abc,abc-3
xyz,xyz-1
xyz,xyz-2
xyz,xyz-3

I want hash table to be created in such a manner where if I look for key i.e abc it will populate
abc-1,abc-2,abc-3 values.

Thanks


shawnhcorey
Enthusiast


Mar 10, 2010, 9:30 AM

Post #2 of 7 (1101 views)
Re: [aceall] Hash tables [In reply to] Can't Post


Code
$hash{$key}{$value}++;


__END__

I love Perl; it's the only language where you can bless your thingy.

Perl documentation is available at perldoc.perl.org. The list of standard modules and pragmatics is available in perlmodlib.

Get Markup Help. Please note the markup tag of "code".


FishMonger
Veteran / Moderator

Mar 10, 2010, 9:31 AM

Post #3 of 7 (1101 views)
Re: [aceall] Hash tables [In reply to] Can't Post

You've posted this beginning level question in the Intermediate forum, so presumably you have Intermediate experience.

What have you tried?

What errors/warnings are you receiving?

What is your script doing that it shouldn't?

What is your script not doing that it should?


aceall
Novice

Mar 10, 2010, 8:52 PM

Post #4 of 7 (1059 views)
Re: [FishMonger] Hash tables [In reply to] Can't Post

hi,

I tried putting the first column and second column in two different arrays. Since, there are repeated items in first column, I took only the unique ones. Now, I have two arrays with first and second fields.

I went through the internet for putting them in Hash tables, but could not find what I was looking for. Hence, I posted over here.

thanks


aceall
Novice

Mar 10, 2010, 9:49 PM

Post #5 of 7 (1055 views)
Re: [aceall] Hash tables [In reply to] Can't Post

Here's the code that I am working on.

P.S: I've removed the arrays. Apologies for not posting this question in beginners forum.


Code
my %hash = (); 
while (<FILE>)
{
s/\r|\n|//g;
s/#.*//;

#split the comma seperated values

($FirstCol,$SecondCol) = split(/\,/) ;
$hash{$FirstCol}{$SecondCol}++;

}

Close FILE;

# the next loop prints the keys/values of the hash
foreach $FirstCol (keys %hash)
{
print "Key: $FirstCol, Value: $hash{$FirstCol}\n";
}
print "\n";


==========================================
Output:
Key: abc, Value: HASH(0x9cb5eb8)
Key: xyz, Value: HASH(0x9cb5e94)


It does not print abc-1,abc-2,abc-3 or xyz-1,xyz-2,xyz-3.



aceall
Novice

Mar 10, 2010, 11:44 PM

Post #6 of 7 (1046 views)
Re: [aceall] Hash tables [In reply to] Can't Post


Code
 
my %hash = ();
while (<FILE>)
{
s/\r|\n|//g;
s/#.*//;

#split the comma seperated values

($FirstCol,$SecondCol) = split(/\,/) ;
push @{$hash{$FirstCol}}, $SecondCol;


}

Close FILE;

# the next loop prints the keys/values of the hash
foreach $FirstCol (sort keys %hash)
{
print "Key: $FirstCol, Value: @{$hash{$FirstCol}}\n";
}
print "\n";


Output:

Key: abc, Value: HASH(0x8a528cc) HASH(0x8af8454) HASH(0x8af9809)
Key: xyz, Value: HASH(0x8a4f374) HASH(0x8af97d4) HASH(0x8af9810)



It is printing the HASH ref instead of HASH value. Following this tutorial: http://perldoc.perl.org/perlreftut.html


aceall
Novice

Mar 11, 2010, 2:09 AM

Post #7 of 7 (1040 views)
Re: [aceall] Hash tables [In reply to] Can't Post

Also, I would like to pop each key and their corresponding values so that I can perform a particular task.

 
 


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

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