I can see you've had problems with the code. It would have been useful to provide an algorithm for calculating what you refer to as Average Precision. I've made a guess at what you've attempting to achieve in the following script. If it's not right, please provide more concrete information.

#!/usr/bin/env perl use strict; use warnings; my %result = (); my $wanted_relaz = q{hyper}; my $last_u = q{}; my $u_count = 0; my $relaz_count = 0; my $precision = 0; my ($rank, $u, $relaz); while (<DATA>) { ($rank, $u, $relaz, undef) = split; if ($u ne $last_u) { if ($last_u) { $result{$last_u} = $precision / $relaz_count; } $last_u = $u; $u_count = 0; $relaz_count = 0; $precision = 0; } ++$u_count; if ($relaz eq $wanted_relaz) { ++$relaz_count; $precision += $u_count / $rank; } } $result{$u} = $precision / $relaz_count; for my $key (sort keys %result) { print qq{$key\t$wanted_relaz\t$result{$key}\n}; } __DATA__ 1 acacia-n hyper tree-n 0.838364743354488 2 acacia-n hyper plant-n 0.740581661563839 3 acacia-n mero wood-n 0.687569086370938 4 acacia-n mero flower-n 0.650909477491374 5 acacia-n coord oak-n 0.610092594991099 6 acacia-n coord pine-n 0.537690234715029 7 acacia-n mero branch-n 0.510899195919917 8 acacia-n mero root-n 0.491198721010063 9 acacia-n coord willow-n 0.481680704877001 1 ant-n hyper animal-n 0.634580215370739 2 ant-n hyper insect-n 0.53621081509255 3 ant-n mero head-n 0.535980012877533 4 ant-n mero body-n 0.505827598873949 5 ant-n coord bee-n 0.481918895790599 1 apricot-n hyper fruit-n 0.76748797529242 2 apricot-n coord apple-n 0.685155565667883 3 apricot-n mero juice-n 0.560337418489082 4 apricot-n coord banana-n 0.559525300446683

Here's the output:

$ pm_avg_precision.pl acacia-n hyper 1 ant-n hyper 1 apricot-n hyper 1

-- Ken


In reply to Re: Iterate over multiple data by kcott
in thread Iterate over multiple data by remluvr

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.