Beefy Boxes and Bandwidth Generously Provided by pair Networks Cowboy Neal with Hat
There's more than one way to do things
 
PerlMonks  

Re: How do I remove the duplicates from the array?

by hdb (Parson)
on Sep 26, 2013 at 12:24 UTC ( #1055823=note: print w/ replies, xml ) Need Help??


in reply to How do I remove the duplicates from the array?

Here is an answer as cryptic (and somewhat wasteful) as your requirements (ignoring LanX extras):

use warnings; use strict; my @array = qw(-20 20 1 1 2 2 2 9 3 -4 -4 5 -20 20 7 7 7); my @array1 = qw( 10 11 7 9 3 3 3 1 3 4 5 5 1 30 8 7 8); for( map{ my $k=$_; [ sort{ $array1[$b]<=>$array1[$a] } grep{ $k eq $a +rray[$_] } 0..$#array ] } @array ) { shift @$_ if @$_==1 or $array1[$$_[0]] ne $array1[$$_[1]]; $array[$_] = $array1[$_] = '' for @$_; } $"=','; print "\@output = (@array);\n\@output1 = (@array1);\n";


Comment on Re: How do I remove the duplicates from the array?
Download Code
Re^2: How do I remove the duplicates from the array?
by LanX (Abbot) on Sep 26, 2013 at 14:04 UTC
    maybe less cryptic?

    DB<154> @a0 = (-20,20,1,1,2,2,2,9,3,-4,-4,5,-20,20); => (-20, 20, 1, 1, 2, 2, 2, 9, 3, -4, -4, 5, -20, 20) DB<155> @a1 = ( 10,11,7,9,3,3,3,1,3, 4, 5,5, 1,30); => (10, 11, 7, 9, 3, 3, 3, 1, 3, 4, 5, 5, 1, 30) DB<156> map { push @{$group{$a0[$_]}},$a1[$_] } 0..$#a0 DB<157> sub highlander { my @sort = sort @{ shift() } ; return $sort[-1]>$sort[0] ? $sort[-1] : undef } DB<158> @high{keys %group } = map { highlander($_) } values %group => (undef, 5, undef, undef, 30, 9, 10, undef) DB<159> map { ($a0[$_],$a1[$_])=() if $a1[$_] != $high{ $a0[$_] } } +0..$#a0 DB<160> \@a0 => [ -20, undef, undef, 1, undef, undef, undef, undef, undef, undef, -4, undef, undef, 20, ] DB<161> \@a1 => [ 10, undef, undef, 9, undef, undef, undef, undef, undef, undef, 5, undef, undef, 30, ]

    Cheers Rolf

    ( addicted to the Perl Programming Language)

      I do not understand the line $sort[-1]>$sort[0] ? $sort[-1] : undef. What if the values are 7, 8, 8? How will it discard the whole thing? I know the OPs spec is rather cryptic but my understanding was, that the values are to be blanked out if the max occurs more than once.

        > I know the OPs spec is rather cryptic but my understanding was, that the values are to be blanked out if the max occurs more than once.

        hmm ... ok my understanding was to discard if all are maximum, see (3,3,3)

        But your definition is indeed closer to the naming highlander, maybe peak was better.

        edit

        For highlander compare with [-2] if there are more than 2 elements.

        BTW: "cryptic" is an euphemism for incompetent ... if you can't express such a problem in words you don't deserve being called a programmer.

        Cheers Rolf

        ( addicted to the Perl Programming Language)

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1055823]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (5)
As of 2014-04-20 13:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (485 votes), past polls