#!/usr/bin/perl use warnings; use strict; open(IN, "data.txt"); my(%seen_each_other_count, %voted_each_other_count, $old_thread_id, @current_user_ids, @current_votes); my $newline=; while ($newline) { chomp($newline); (my $user_id, my $thread_id, my $vote) = split(/\t/, $newline); $old_thread_id=$thread_id; push @current_user_ids, $user_id; push @current_votes, $vote; $newline=; chomp($newline); ($user_id, $thread_id, $vote) = split(/\t/, $newline); while ($thread_id==$old_thread_id) { push @current_user_ids, $user_id; push @current_votes, $vote; $newline=; chomp($newline); ($user_id, $thread_id, $vote) = split(/\t/, $newline); } for (my $i=0; $i<=$#current_user_ids; $i++) { for (my $j=$i; $j<=$#current_user_ids; $j++) { #When $j==$i this just counts how many times you appear, and how many times you vote $seen_each_other_count{$current_user_ids[$i]}{$current_user_ids[$j]}++; if ($current_votes[$i]==1 && $current_votes[$j]==1) { $voted_each_other_count{$current_user_ids[$i]}{$current_user_ids[$j]}++; } } } $old_thread_id=$thread_id; @current_user_ids=(); @current_votes=(); push @current_user_ids, $user_id; push @current_votes, $vote; $newline=; chomp($newline); } #Since the data is read down, the total number of times that #one saw two is equal to the number of times that we counted one seeing two, #plus the number of times we counted two seeing one sub numerically { $a <=> $b }; open(OUT, ">PMTEST.txt"); print OUT "user1\tuser2\tsaw_count\tvote_count\n"; foreach my $first (sort numerically (keys %seen_each_other_count)) { my $ref=$seen_each_other_count{$first}; foreach my $second (sort numerically (keys %$ref)) { my ($final_count, $final_votes); if ($first==$second) { $final_count=$seen_each_other_count{$first}{$second}; $final_votes=$voted_each_other_count{$first}{$second}; } else { $final_count=$seen_each_other_count{$first}{$second}+$seen_each_other_count{$second}{$first}; $final_votes=$voted_each_other_count{$first}{$second}+$voted_each_other_count{$second}{$first}; } $final_votes||=0; print OUT "$first\t$second\t$final_count\t$final_votes\n"; } }