Hi,
I am stuck at this problem where part of my code would store all the websites that has been accessed by a user. I pull these values from a log file. I want to create a HASH of HASHES ? (Please correct me if this is not the right approach) where I would store all the hits to website with time and the page that was accessed. For example:
log file could look something like:
http://www.yahoo.com/blah.html : 12:54
http://www.yahoo.com/blah2.html : 12:54
http://www.yahoo.com/blah.html : 12:55
http://www.google.com/one.html : 12:55
http://www.google.com/two.html : 12:55
http://www.google.com/three.html : 12:56
http://www.google.com/two.html : 12:57
- Notice that there is nothing really UNIQUE that I could use as a KEY ? My question is : How should I store this data in a HASH where I can see all the pages that were accessed by a user and their respected times?
Any help is highly appreciated.
I guess you would use the top level domain name as the top level hash keys and the individual pages would be sub hashes and each sub hash would have even more sub hashes, or arrays, to hold the information you want. Are you stuck on writing actual code? What have you tried so far? What does the real data look like?
Thx Kevin but I can't seem to picture how to write the structure , populate it and then output it.
So this part of code is going to be part of bigger code , where what I am ultimately trying to do is show all the SITES a person visited outputted to a webpage with timings etc but in a TREE sort of fashion, something like:
Google
|______ search.html : time
|______ search2.html : time
|______ search3.html : time
So in my bigger code , i am trying to temporarily store these values in ARRAY and eventually would spit it out on a webpage.
I am stuck at writing a structure/code for this which will give me the desired result. First basically put these value in the best suited structure and then output the values of the structure.
The real data actually looks like:
The log file has these:
www.google.com/test1.asp:11:27
www.google.com/test2.asp:11:27
www.google.com/test2.asp:11:27
www.google.com/test2.asp:11:28
Thanks in advance.
I generally do not write any code for someone that has demonstrated no effort to first try and solve, or to at least start, there own code. But here something that is meant only as an example of how to create a hash of hash of arrays from your log file:
my %hash;
while(<DATA>){
chomp;
my ($domain,$page,$h,$m) = split(/[\/:]/);
push @{$hash{$domain}{$page}},"$h:$m";
}
foreach my $domain (keys %hash) {
print "$domain\n";
foreach my $page (keys %{$hash{$domain}}){
print "\t$page @{$hash{$domain}{$page}}\n";
}
}
__DATA__
www.google.com/test1.asp:11:27
www.google.com/test2.asp:11:27
www.google.com/test2.asp:11:27
www.google.com/test2.asp:11:28
There are any number of data strucutes you could try and use, which one you eventually use depends on your programming requirements. A more complete hash of hashes/arrays might be better suited for the purpose but I like to keep data structures as simple as possible for the task at hand. If you know you will need to scale up the program in the future you could add any fields to the data structure now that will be used later.
Kevin,
This is great help. I really really appreciate you going out of way to help me design better structure in my prog. I am already working on DHTML aspect of this tree view.
Once again. Big Thanks.
-Dabheeruz