Hi,
I have this problem which i could prolly do by using heaps of foreach loops, but am wondering if there is a quicker, easier and cleaner way to do it?
I would like to be able to create a tally of matching arrays. The problem is prolly easier if I demonstrate...
I have two hashes of arrays that look thus:
# an array of terms used to create hash1
my @terms = ( 'male','female','child' );
# hash1 of term arrays
my %terms = ();
$terms{1} = [ 'male' ];
$terms{2} = [ 'female' ];
$terms{3} = [ 'child' ];
$terms{4} = [ 'male', 'female' ];
$terms{5} = [ 'male', 'child' ];
$terms{6} = [ 'female', 'child' ];
$terms{7} = [ 'male', 'female', 'child' ];
# hash2 of term occurrences
my %occurs = ();
$occurs{111} = [ 'male' ];
$occurs{112} = [ 'male' ];
$occurs{113} = [ 'male', 'child' ];
$occurs{114} = [ 'female' ];
$occurs{115} = [ 'child', 'female' ];
My objective is to come out with one hash that says how often each "occurrence of a combination of terms" from hash1, occurred in hash2. For example:
my %tally = ();
# Male on its own appeared twice
$tally{hash1_terms1} = 2;
# Female on its own appeared once
$tally{hash1_terms2} = 1;
# Child on its own appeared twice
$tally{hash1_terms3} = 0;
# and so on...
I am hoping that someone out there has more logic than me and can see an easy way to do this. Any help would be appreciated.
Cheers,
Reagen
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.