Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Re: Element Count from Array to HoH

by ivancho (Hermit)
on May 26, 2005 at 06:29 UTC ( [id://460519]=note: print w/replies, xml ) Need Help??


in reply to Element Count from Array to HoH

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

Replies are listed 'Best First'.
Re^2: Element Count from Array to HoH
by monkfan (Curate) on May 26, 2005 at 07:15 UTC
    Hi Ivancho,
    Q: Perhaps you ought to be increasing {TTAT}{I1}, ie {$item2}{$tid}
    A: I can't do that, cause it will miss counting "TTTT". if what you mean is changing them to this:
    $$transaction_map_ref{$item2}{$tid}++;
    Q:or possibly {TTTT}{I2}?
    A:I don't get what you mean by that. What's the difference with the above statement.
    Sorry..I'm rather slow here..
    Q:can you have repeated elements in your array, and if so, what should be the behaviour?
    A: Yes if that's the case, as long as condition "hd<=$d", they should be counted.
    I hope that clarifies the problem.
    Regards,
    Edward
      Yeah, my bad, I only meant {TTTT}{I2} - which is what my code does, I shouldn't have asked about {TTAT}{I1}..

      My understanding is as follows. You get a pair "TTAT", "I2", and you say to yourself 'But this TTAT might actually be TTTT - so let's increase {TTTT}{I2} for good measure.. We'll increase {TTAT}{I2} too, when we get there...'

      If this is what you want to do, then my snippet should work fine.. The {map {(split)... line saves the unique 4 letter codes we have. Then for each line '$tid $item',
      grep {hd($item,$_) <= $d} keys %$trans_map_ref
      gives us which of our 4 letter codes $item might be. For each of them, we increase their {$tid} field.

      I think the problem here is that your function tries to do everything at once.. Maybe you can split it in 2 pieces - first find all pairs of neighbours between the 4 letter codes, then go through your array and for every item with a tid, also increase this tid for it's neighbours..

      hopefully this isn't just more mud...

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://460519]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (5)
As of 2024-04-25 14:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found