in reply to Re: Merging Hashes adding multiple values for keys, and replacing others
in thread Merging Hashes adding multiple values for keys, and replacing others

Thanks a lot CombatSquirrel, that was very close to what I'm looking for. I did
rewrite the date part, as I have decided to use unix timestamp, as that simplifies things some.
And may I say you RoCk! here's, the updated code:
#!/usr/bin/perl # use warnings; use strict; use Data::Dumper; sub apache2epoch() { use POSIX; my $datetime=$ARGV[0]; my $i=0; my %months; %months = map { $_ => $i++ } ("Jan","Feb","Mar","Apr","May","Jun", "Jul","Aug","Sep","Oct","Nov","Dec"); $_[0] =~ m((\d+)/(\w+)/(\d+):(\d+):(\d+):(\d+)); return &mktime($6,$5,$4,$1,$months{$2},$3-1900,0,0,-1); } + + my %users; while (<DATA>) { my ($user, $time, $primary, $secondary, $views, $clicks, $sessions, $mode, $zip) = split; if ($users{$user}) { if ((&apache2epoch($time))-(&apache2epoch(($users{$user}->{LastVisitTi +me}))) >1800 ) { $users{$user}->{LastVisitTime} = $time; } } else { $users{$user}->{LastVisitTime} = $time; $users{$user}->{PrimaryCategory} = $primary; $users{$user}->{SecondaryCategory} = $secondary; $users{$user}->{MarketingMode} = $mode; $users{$user}->{ZIP} = $zip; } $users{$user}->{PageViews} += $views; $users{$user}->{MerchantClicks} += $clicks; $users{$user}->{Sessions} += $sessions; } print Dumper(\%users); + __DATA__ 188c0530ac92475a 01/Jan/2003:16:00:02 Computers Computers 1 1 1 no_mod +e 44614 0ce46e475f94ecb9 01/Jan/2003:16:00:08 Computers Computers 1 0 1 no_mod +e 00000 189e45678964fcf6 01/Jan/2003:16:00:07 Electronics Electronics 1 0 1 no +_mode 00000 189a4a75d0cbad03 01/Jan/2003:16:00:01 No_category No_category 1 0 1 no +_mode 00000 18a416ba3d3c7a8d 01/Jan/2003:16:00:12 No_category No_category 2 0 1 no +_mode 00000 18aa11982e30e1ef 01/Jan/2003:16:00:07 No_category No_category 1 0 1 no +_mode 00000 0ce46e475f94ecb9 01/Jan/2003:17:00:08 Computers Computers 1 0 1 no_mod +e 00000 189a4a75d0cbad03 01/Jan/2003:17:00:01 No_category No_category 1 0 1 no +_mode 00
btw, on the CompDates sub, if I encounter the same $user it gets the errors
Use of uninitialized value in numeric comparison (<=>) at ./overall.pl line 28, <DATA> line 7.
thanks again CombatSquirrel, and Cheers to you too

~vili
sniff sniff 802.11
  • Comment on Re: Re: Merging Hashes adding multiple values for keys, and replacing others
  • Download Code