in reply to removing reversed elements

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.

Replies are listed 'Best First'.
Re^2: removing reversed elements
by ramdat (Initiate) on Feb 02, 2011 at 09:20 UTC
    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.
        This is nice but it misses the case where the pair of numbers is equal, as the first test will pass, but the second will fail. I believe this small modification will cover that case though
        use Data::Dump qw[ pp ]; my @v = ([100, 200],[200, 300],[200, 100], [200, 200]);; my %uniq; my @r = grep{ ++$uniq{ join $;, sort @$_ } == 1 } @v;; pp @r;; #([100, 200], [200, 300], [200, 200])
        - Miller
        I am sorry..i am getting following error message:

        Can't locate Data/Dump.pm in @INC (@INC contains: /etc/perl /usr/local/lib/perl/5.10.1 /usr/local/share/perl/5.10.1 /usr/lib/perl5 /usr/share/perl5 /usr/lib/perl/5.10 /usr/share/perl/5.10 /usr/local/lib/site_perl /usr/local/lib/perl/5.10.0 /usr/local/share/perl/5.10.0 .) at test.pl line 1. BEGIN failed--compilation aborted at test.pl line 1.

        The code is very high level to people like me..if you would not mind, could you comment it please..

        Browser: Once again, your solution is a thing of beauty. I bow to your perliness.
Re^2: removing reversed elements
by bart (Canon) on Feb 02, 2011 at 12:40 UTC
    I'm curious: why do you tend to use double semicolons (";;") at the end of your statements?