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

In reply to Re: Element Count from Array to HoH by ivancho
in thread Element Count from Array to HoH by monkfan

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.