Incrementing a Multi-dimensional Hash Array 
Author Message
 Incrementing a Multi-dimensional Hash Array

Hi, I have a file of user-id's and reports they have accessed.

user1,report1
user1,report1
user1,report8
user2,report3
user2,report8
etc....

I would like to create a report in Perl that would show me the users and
the number of times they have accessed any given report.

I know that I can increment a single hash array like this:

While (<FILE>) {
  ($userid,$reportid) = split(/,/);
   $total{$userid}++;

Quote:
}

This would give me the total number of times a given user accessed ANY
report.  But how do I create a multi-dimensional array and print off the
totals for a given user for each report?

$total{$userid}{$report}++;
does not work.

perldsc did not appear to be any help.  What am I missing???

Thanks
Kevin



Tue, 27 Feb 2001 03:00:00 GMT  
 Incrementing a Multi-dimensional Hash Array

Quote:

>Hi, I have a file of user-id's and reports they have accessed.
> [snip]

>$total{$userid}{$report}++;
>does not work.

Funny, it should.  Anyway, see how different this appears from your
code.

#!/usr/local/bin/perl -w
use strict;

my %total = ();

while (<DATA>) {
    chomp;
    my ($user, $report) = split /,/;
    $total{$user}{$report}++;

Quote:
}

for my $user (keys %total) {
    print "$user\n";
    for my $report (keys %{$total{$user}}) {
        print "\t$report = $total{$user}{$report}\n";
    }

Quote:
}

__END__
user1,report1
user1,report1
user1,report8
user2,report3
user2,report8

--



Tue, 27 Feb 2001 03:00:00 GMT  
 Incrementing a Multi-dimensional Hash Array

Quote:

> Hi, I have a file of user-id's and reports they have accessed.

> user1,report1
> user1,report1
> user1,report8
> user2,report3
> user2,report8
> etc....

> I would like to create a report in Perl that would show me the users and
> the number of times they have accessed any given report.

> I know that I can increment a single hash array like this:

> While (<FILE>) {
>   ($userid,$reportid) = split(/,/);
>    $total{$userid}++;
> }

> This would give me the total number of times a given user accessed ANY
> report.  But how do I create a multi-dimensional array and print off the
> totals for a given user for each report?

> $total{$userid}{$report}++;
> does not work.

what does "does not work" mean? maybe you meant $reportid there?
this works fine:

#!/usr/bin/perl -w
use strict;
my %hash;
while(<DATA>){
    chomp;
    my ($user,$report)=split /,/;
    $hash{$user}{$report}++;

Quote:
}

foreach my $user (keys %hash){
    print "$user:\n";
    foreach my $report (keys %{$hash{$user}}){
        print "\t$report: $hash{$user}{$report}\n";
    }
Quote:
}

__DATA__
user1,report1
user1,report1
user1,report8
user2,report3
user2,report8

hope it helps
regards
andrew



Tue, 27 Feb 2001 03:00:00 GMT  
 Incrementing a Multi-dimensional Hash Array
[Posted to comp.lang.perl.misc and a copy mailed.]



Quote:
> Hi, I have a file of user-id's and reports they have accessed.

> user1,report1
> user1,report1
> user1,report8
> user2,report3
> user2,report8
> etc....

> I would like to create a report in Perl that would show me the users and
> the number of times they have accessed any given report.

> I know that I can increment a single hash array like this:

> While (<FILE>) {
>   ($userid,$reportid) = split(/,/);
>    $total{$userid}++;
> }

> This would give me the total number of times a given user accessed ANY
> report.  But how do I create a multi-dimensional array and print off the
> totals for a given user for each report?

> $total{$userid}{$report}++;
> does not work.

Why not?  Maybe you forgot to chomp the $reportid.

#!/usr/local/bin/perl -w
use strict;

my %total;

while (<DATA>) {
        chomp;
        my ($userid, $reportid) = split /,/;
        $total{$userid}{$reportid}++;

Quote:
}

my $userid;
foreach $userid (sort keys %total) {
        foreach (sort keys %{$total{$userid}}) {
                print "$userid,$_: $total{$userid}{$_}\n";
        }
Quote:
}

__END__
user1,report1
user1,report1
user1,report8
user2,report3
user2,report8

Output:

user1,report1: 2
user1,report8: 1
user2,report3: 1
user2,report8: 1

--
(Just Another Larry) Rosler
Hewlett-Packard Laboratories
http://www.hpl.hp.com/personal/Larry_Rosler/



Tue, 27 Feb 2001 03:00:00 GMT  
 Incrementing a Multi-dimensional Hash Array
On Fri, 11 Sep 1998 14:57:26 -0700, Larry Rosler wrote :

Quote:
>[Posted to comp.lang.perl.misc and a copy mailed.]



>> Hi, I have a file of user-id's and reports they have accessed.

>> user1,report1
>> user1,report1
>> user1,report8
>> user2,report3
>> user2,report8
>> etc....

>> I would like to create a report in Perl that would show me the users and
>> the number of times they have accessed any given report.

>> I know that I can increment a single hash array like this:

>> While (<FILE>) {
>>   ($userid,$reportid) = split(/,/);
>>    $total{$userid}++;
>> }

>> This would give me the total number of times a given user accessed ANY
>> report.  But how do I create a multi-dimensional array and print off the
>> totals for a given user for each report?

>> $total{$userid}{$report}++;
>> does not work.

>Why not?  Maybe you forgot to chomp the $reportid.

>#!/usr/local/bin/perl -w
>use strict;

>my %total;

>while (<DATA>) {
>    chomp;
>    my ($userid, $reportid) = split /,/;
>    $total{$userid}{$reportid}++;
>}

>my $userid;
>foreach $userid (sort keys %total) {
>    foreach (sort keys %{$total{$userid}}) {
>            print "$userid,$_: $total{$userid}{$_}\n";
>    }
>}
>__END__
>user1,report1
>user1,report1
>user1,report8
>user2,report3
>user2,report8

>Output:

>user1,report1: 2
>user1,report8: 1
>user2,report3: 1
>user2,report8: 1

Its funny when you think of it - evryone goes around incanting
TIMTOWTDI yet three people post virtually identical pieces of code ;-}

Even funnier that I cant think of more than way to do it.

/J\
--
Jonathan Stowe
Some of your questions answered:
<URL:http://www.btinternet.com/~gellyfish/resources/wwwfaq.htm>



Wed, 28 Feb 2001 03:00:00 GMT  
 Incrementing a Multi-dimensional Hash Array

Quote:

[snip]

> Its funny when you think of it - evryone goes around incanting
> TIMTOWTDI yet three people post virtually identical pieces of code ;-}

> Even funnier that I cant think of more than way to do it.

TIMTOWTDI:

#!/usr/bin/perl -w
use strict;
my %hash;
while(<DATA>){
    chomp;
    $hash{$_}++;

Quote:
}

foreach my $key (sort keys %hash){
    print "$key: $hash{$key}\n";
Quote:
}

__DATA__
user1,report1
user1,report1
user1,report8
user2,report3
user2,report8

But the poster had explicitly mentioned incrementing a
multi-dim hash, so perhaps this doesn't count.

regards
andrew



Wed, 28 Feb 2001 03:00:00 GMT  
 Incrementing a Multi-dimensional Hash Array


Quote:
> While (<FILE>) {
>   ($userid,$reportid) = split(/,/);
>    $total{$userid}++;
> }

[snip]

Quote:
> $total{$userid}{$report}++;
> does not work.

Not even if you use $reportid, instead of $report?

Maybe you should consider starting to run perl with the -w flag, and
maybe program with use strict. Things like this are much less likely
to happen then.

Martien
--
Martien Verbruggen                  |
Webmaster www.tradingpost.com.au    | That's not a lie, it's a terminological
Commercial Dynamics Pty. Ltd.       | inexactitude.
NSW, Australia                      |



Thu, 01 Mar 2001 03:00:00 GMT  
 
 [ 7 post ] 

 Relevant Pages 

1. help: multi-dimensional hash from flat array

2. multi-dimensional arrays with hashes help please

3. Weird Hash w/ n-dimensional two-dimensional array !!!!!!

4. arrays, multi-dimensional arrays, pointers and such

5. Associative arrays and multi-dimensional arrays.

6. Multi-Dimensional arrays, DBMs, and Assoc. Arrays

7. Extracting an array out of a multi-dimensional associative array

8. Confused about multi-dimensional hashes

9. Problems undef'ing parts of multi-dimensional hashes

10. Multi-dimensional hash question

11. dealing with multiple entires in a multi-dimensional hash

12. Problem accessing individual elements of multi-dimensional hash

 

 
Powered by phpBB® Forum Software