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

How about the following code:
#!perl use strict; use warnings; use Data::Dumper; my %monthLookup = ( Jan => 1, Feb => 2, Mar => 3, Apr => 4, May => 5, Jun => 6, Jul => 7, Aug => 8, Sep => 9, Oct => 10, Nov => 11, Dec => 12 ); sub CompDates($$) { my ($first, $second) = @_; $first = [($first =~ m!(\d{2])/(\w{3})/(\d{4}):(\d{2}):(\d{2}):(\ +d{2})!)]; $second = [($second =~ m!(\d{2])/(\w{3})/(\d{4}):(\d{2}):(\d{2}):(\ +d{2})!)]; $first->[1] = $monthLookup{$first->[1]}; $second->[1] = $monthLookup{$second->[1]}; return (($first->[0] <=> $second->[0]) || ($first->[1] <=> $second->[1]) || ($first->[2] <=> $second->[2]) || ($first->[3] <=> $second->[3]) || ($first->[4] <=> $second->[4]) || ($first->[5] <=> $second->[5]) ); } my %users; while (<DATA>) { my ($user, $time, $primary, $secondary, $views, $clicks, $sessions, $mode, $zip) = split; if ($users{$user}) { if (CompDates($users{$user}->{LastVisitTime}, $time) < 0) { $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__ 0ce46e475f94ecb9 01/Jan/2003:16:00:08 Computers Computers 1 0 1 no_mod +e 00000 188c0530ac92475a 01/Jan/2003:16:00:02 Computers Computers 1 1 1 no_mod +e 44614 189a4a75d0cbad03 01/Jan/2003:16:00:01 No_category No_category 1 0 1 no +_mode 00000 189e45678964fcf6 01/Jan/2003:16:00:07 Electronics Electronics 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
This should do it, as far as I understood your problem. Post a reply if it does not.
Cheers, CombatSquirrel.
  • Comment on Re: Merging Hashes adding multiple values for keys, and replacing others
  • Download Code

Replies are listed 'Best First'.
Re: Re: Merging Hashes adding multiple values for keys, and replacing others
by vili (Monk) on Aug 22, 2003 at 18:09 UTC
    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