ramdat has asked for the wisdom of the Perl Monks concerning the following question:

hi all..i am newbie to perl..i have an array which has interactions like(100 200,200 300,200 100,.....)But what i need is only (100 200,200 300) but not 200 100 interaction.I mean i dont want reversed elements..i am not able to figure it out..could anyone please help me out....

Replies are listed 'Best First'.
Re: removing reversed elements
by BrowserUk (Patriarch) on Feb 02, 2011 at 08:57 UTC

    You have an array of pairs of values and you only want to retain those where the second value of each pair is greater than (or equal to?) the first:

    @v = ([100, 200],[200, 300],[200, 100]);; @r = grep{ $_->[0] < $_->[1] } @v;; pp @r;; ([100, 200], [200, 300])

    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
      No sorry..actually i meant that (100 200) pair and (200 100)pair are both same for me..so i want only either (100 200) or (200 100)pair in the output.So if i have elements (X Y) it is same as (Y X) too..I need only one pair from the both..it can be any thing...

        Okay. Try:

        use Data::Dump qw[ pp ]; my @v = ([100, 200],[200, 300],[200, 100]);; my %uniq; my @r = grep{ ++$uniq{ join $;, $_->[0], $_->[1] } == 1 and ++$uniq{ join $;, $_->[1], $_->[0] } == 1 } @v;; pp @r;; ([100, 200], [200, 300])

        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        "Science is about questioning the status quo. Questioning authority".
        In the absence of evidence, opinion is indistinguishable from prejudice.
      I'm curious: why do you tend to use double semicolons (";;") at the end of your statements?
Re: removing reversed elements
by locked_user sundialsvc4 (Abbot) on Feb 02, 2011 at 15:28 UTC

    I do not know how many pairs you have.   If you have enough pairs that you could reasonably store all of them in memory, a hash-table is a convenient way to look for duplicates.

    A more generalized solution would be to write all (A,B) pairs to one external disk file, and all (B,A) pairs to a second.   Using an external disk sort, sort both of the files.   Now, merge the two files and discard all matching entries.   You only need to consider the one tuple that you have most-recently read from each file:   if they are identical, throw them away (and when you decide to do this, also throw away all identical occurrences ... since the files are sorted, they will all be adjacent in each file).

    (The latter technique, which goes straight back to COBOL and, in fact, to the days before digital computers, will work for arbitrary quantities of data, has a completion time curve that is mostly-linear, and requires almost no RAM.)

    For smaller quantities of data, which are known to fit comfortably in available RAM, the same technique can be used with Perl in-memory lists and its sort verb.