I think that
last; bit is messing your results.. Do you really want to break the moment you found a match? In this case, you'll find yourself for half of the array, and your neighbour for the other half - sounds confusing to me..
Also, from your example it's not clear what you are counting - if we had a couple of rows like "I1 TTTT", and "I2 TTAT", currently you'd increase {TTTT}{I1}, when you see "I2 TTAT" - is that what you want? Perhaps you ought to be increasing {TTAT}{I1}, ie {$item2}{$tid}, or possibly {TTTT}{I2}?
Last question, can you have repeated elements in your array, and if so, what should be the behaviour?
Ok, now I'll suggest a rewrite, based on my assumptions what this should be doing, but don't be too disappointed if they were wrong. I prefer returning a new ref out of the sub, rather than passing one in:
my $transaction_map_mismatch = get_trans_array_mismatch(\@to_proc,$d);
print Dumper $transaction_map_mismatch;
#--------Sub------------
sub get_trans_array_mismatch {
my $transaction_array = shift;
my $d = shift;
my $trans_map_ref = {map {(split)[1]=>{}} @$transaction_array};
foreach (@{$transaction_array}) {
my ($tid, $item) = split;
foreach (grep {hd($item,$_) <= $d} keys %$trans_map_ref) {
$trans_map_ref->{$_}{$tid}++;
}
}
return $trans_map_ref;
}
Not efficient, in the slightest, but I think it does the job, or at least matches your desired output