#!/bin/bash
declare -A ht
declare -i i

hist=$1

if [[ ! -a $hist ]]; then
  echo "USAGE: histmap <histfile>"
fi

((i=0))

# load hash table with reverse of hist file
# Don't use pipe here to avoid subshell so that hash table elements are 
# in main process not a subshell
while read cnt sum file
do
   if [[ -n ${ht[$sum]} ]]; then
       echo "ERROR: $i: $sum already defined ... $sum :- ${ht[$sum]}" > /dev/stderr
   else
     ht[$sum]="$i"
   fi
   ((i++))
done < <(sort -n -r -k1,1 $hist)

while read sum rest
do
   if [[ -z "${ht[$sum]}" ]]; then
      echo "ERROR: $sum $rest not in hash table" > /dev/stderr
   else
      echo ${ht[$sum]}
   fi
done

    
