Your text and example differ slightly. In multiple places the text indicates word 3 comes before words 1 and 2, but your example shows word 3 coming after words 1 and 2. I am assuming you want the latter, but in reality it doesn't impact the choice of data structure.

There are a lot of ways to approach this, but I might use something like

$hash{$word1}{$word2}{$word3} = $count
where $count is the number of times $word3 is found immediately after (or before) words 1 and 2.

Note that I did not store the percentage, which can be calculated after all of the counts have been accumulated. If you wanted a more complicated structure to facilitate that, you could do something like

$hash{$word1}{$word2}{$word3} = { count => $count, freq => $frequency }
or perhaps even better
$hash{$word1}{$word2} = { total_counts => $tot_counts, words => { $word3 => $count, } }
In the last example you would increment the count for the appropriate $word3 as well as the total_counts for the word pair. This would allow you to easily calculate the frequency for any given $word3 on the fly.*

To populate the data structure, simply identify words 1, 2, and 3 and then do

$hash{$word1}{$word2}{total_counts}++; $hash{$word1}{$word2}{words}{$word3}++;
(or similar, depending on which one you choose).

These structures may be able to be simplified, depending on your requirements. On the other hand, if you had a lot of data and expected the structure to become quite large, a database may be a better choice.

Note that I did not use any arrays. Since the description of the problem implied that you would be looking up data by $word, a hash seemed more appropriate. An array (or an array of hash refs) would require you to search through the contents of the array to find the $word of interest, and depending on the size of the arrays that could be a very inefficient approach.

*If you really wanted to store the frequency of each word and you didn't need a running total, I'd suggest doing the calculation at the end and using $word3 => { count => $count, freq  => $frequency } rather than $word3 => $count, which would make adding the frequencies later a bit more straightforward.


In reply to Re: Trying to select the best data structure by bobf
in thread Trying to select the best data structure by chinamox

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.