Hi Monks, I was looking for a solution to my problem of getting the top x matches for each error category, when I stumble upon the below example given by an expert. In my case, I already have the entire table stored in an array - @dataarray. I used a hash to get the count for each category. Colum 3 of the array holds the error code. The below is a simple code showing how I get the count for each category.
my %error = ( 'disk' => 0, 'cpu' => 0, 'hdd' => 0, 'graphics' => 0, 'keyboard' => 0, '' => 0 #no error code ); for my $i (0 .. $#dataarray) { $error{$dataarray[$i][3]}++; }
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. Nefertiti
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]]; }

In reply to Revisiting the "Getting top x matches" thread by nefertiti

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.