title: Pig: Get index in nested foreach
body: <p>I have a pig script with code like :</p>
  
  <pre><code>scores = LOAD 'file' as (id:chararray, scoreid:chararray, score:int);
  scoresGrouped = GROUP scores by id;
  top10s = foreach scoresGrouped{
      sorted = order scores by score DESC;
      sorted10 = LIMIT sorted 10;
      GENERATE group as id, sorted10.scoreid as top10candidates;
  };
  </code></pre>
  
  <p>It gets me a bag like</p>
  
  <pre><code> id1, {(scoreidA),(scoreidB),(scoreIdC)..(scoreIdFoo)}
  </code></pre>
  
  <p>However, I wish to include the index of items as well, so I'd have results like</p>
  
  <pre><code> id1, {(scoreidA,1),(scoreidB,2),(scoreIdC,3)..(scoreIdFoo,10)}
  </code></pre>
  
  <p>Is it possible to include the index somehow in the nested foreach, or would I have to write my own UDF to add it in afterwards?</p>