in reply to Trying to select the best data structure

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.

Replies are listed 'Best First'.
Re^2: Trying to select the best data structure
by chinamox (Scribe) on Oct 29, 2006 at 13:53 UTC

    Thank you very much for your time and thoughts. I went back after reading your excellent explination and looked over the data structures cookbook. I now feel much more informed about Perl's data structures and understand why arrays would not work well in this situation.

    cheers,
    -mox