nefertiti has asked for the wisdom of the Perl Monks concerning the following question:
Column 0 and column 1 holds the computer location and name (respectively). They are unique only as a pair. Can any experts advice how I can get the top x matches for each category? In addition, I also need the error occurance count for each of the top x computer location/name. Thanks. Nefertitimy %error = ( 'disk' => 0, 'cpu' => 0, 'hdd' => 0, 'graphics' => 0, 'keyboard' => 0, '' => 0 #no error code ); for my $i (0 .. $#dataarray) { $error{$dataarray[$i][3]}++; }
my $query = $db->prepare("select code, username, userid from transac +tions"); $query->execute(); my %code = map { $_ => [] } 0 .. 13; while (my $r = $qeury->fetchrow_hashref()) { my $c = $$r{code}; push @{$code{$c}}, $r; } # For each code $c, $code{$c} is now an arrayref which # contains the relevant records (as hashrefs). The # number of elements in the array equals the number # of occurances. Now, for the second part... my %topfive; for my $c (keys %code) { my %user; for my $row (@{$code{$c}}) { # Note: this assumes that the userid field # is unique to each user. $user{$$row{userid}}{count}++; $user{$$row{userid}}{name} = $$row{username}; } $topfive{$c} = [(sort { $$b[2] <=> $$a[2] } map { [$_, $user{$_}{name}, $user{$_}{count}] } keys %user)[0 .. 4]]; }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Revisiting the "Getting top x matches" thread
by Anonymous Monk on Sep 18, 2008 at 06:25 UTC | |
by nefertiti (Initiate) on Sep 18, 2008 at 07:03 UTC |