in reply to Choosing the right datastructure

The following appears to do the trick. I've just dumped the results with Data::Dumper, so it's up to you to figure out who you want to format them.

#!/usr/bin/perl -w use strict; $|++; my %source; # process file while (<DATA>) { my ($source, $action, $sub_action) = split; my $source_action = $source . "||" . $action; # sub_action isn't required to appear $source{ $source }{ count }++; $source{ $source }{ $action }{ count }++; $source{ $source }{ $action }{ $sub_action }{ count }++ if $sub_acti +on; } use Data::Dumper; print Dumper \%source; __DATA__ source1 QUEUED source1 QUEUED source1 CLICK linkid1 source1 CLICK linkid1 source1 CLICK linkid2 source2 QUEUED source2 CLICK linkid1 source2 CLICK linkid1 source2 CLICK linkid2

Cheers,
Ovid

Update: If Data::Dumper output bugs you, here's a quick hack at printing the results.

foreach my $source ( sort keys %source ) { print "$source\n\tcount: $source{$source}{count}\n"; foreach my $action ( sort keys %{$source{$source}} ) { next if $action eq 'count'; print "\t$action\n\t\tcount: $source{$source}{$action}{count}\n"; foreach my $sub_action (sort keys %{$source{$source}{$action}} ) { next if $sub_action eq 'count'; print "\t\t$sub_action\n\t\t\tcount: $source{$source}{$action}{$ +sub_action}{count}\n"; } } }

Join the Perlmonks Setiathome Group or just click on the the link and check out our stats.

Replies are listed 'Best First'.
Re: Re: Choosing the right datastructure
by dug (Chaplain) on Apr 07, 2002 at 04:04 UTC
    Thanks.
    My initial confusion came from thinking that:
    my %source; my %action; my ($source, $action) = qw(test_source test_action); $source{$source} = { $action{$action}++ }; $source{$source} = { $action{$action}++ };
    would give me a hash called %source with each key having a value of the hash "%action", with it's keys and values.
    It output:
    $VAR1 = 'test_source';
    $VAR2 = {
              '1' => undef
            };
    $VAR1 = 'test_action';
    $VAR2 = '2';
    

    where I was looking for:
    $VAR1 = 'test_source';
    $VAR2 = {
              'test_action' => 2
            };
    
    It makes Perl-fect sense, though, that it would set the value of $source{$action} to true, while setting the keys and values of %action_hash.

    Thanks for helping me grok this.
      dug