What you've got sounds like a directed graph, and CPAN has the module Graph. I'm also using Text::CSV to make parsing and output more robust (also install Text::CSV_XS for speed).

input.csv:

personA,personB,10 personB,personA,190 personA,personC,23 personA,personD,43 personE,personF,10

Code:

#!/usr/bin/env perl use warnings; use strict; use Text::CSV; use Graph; my $filename = 'input.csv'; my $g = Graph->new(directed=>1); open my $fh, '<', $filename or die "$filename: $!"; my $csv = Text::CSV->new({ binary=>1, auto_diag=>2, eol=>$/ }); while ( my $row = $csv->getline($fh) ) { my ($author1, $author2, $interactions) = @$row; $g->set_edge_attribute( $author1, $author2, # auto-creates edge 'interactions', $interactions ); } $csv->eof or $csv->error_diag; close $fh; for my $e ($g->edges) { my ($author1, $author2) = @$e; next if $g->has_edge($author2, $author1); my $interactions = $g->get_edge_attribute( $author1, $author2, 'interactions' ); $csv->print(select, [ $author1, $author2, $interactions ]); }

Output:

personE,personF,10 personA,personD,43 personA,personC,23

Update: The above does not handle the case of duplicates in the input. Is that a concern for you, and if yes, what happens with the "interactions"? Are they supposed to be summed up?


In reply to Re: print lines which are not reverse duplicates by haukex
in thread print lines which are not reverse duplicates by Maire

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.