Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

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

by choroba (Abbot)
on Sep 25, 2013 at 20:09 UTC ( #1055743=note: print w/ replies, xml ) Need Help??


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

Your specification is very unlclear. That is what I made of it: you want to keep a tuple in both arrays if the number in the top array (key) appears only once or the corresponding value from array 2 is greater than all corresponding values to the same key.

#!/usr/bin/perl 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); my %hash; for my $i (0 .. $#array) { my $first = $array[$i]; my $second = $array1[$i]; if (not exists $hash{$first}{highest}) { # The key seen for the first time. $hash{$first}{highest} = $second; } else { # The highest value seen again. $hash{$first}{delete} = 1 if $second == $hash{$first}{highest} +; # A new highest value. if ($second > $hash{$first}{highest}) { $hash{$first}{highest} = $second; $hash{$first}{delete} = 0; } } } # Delete repeated max values. for my $key (keys %hash) { delete $hash{$key} if $hash{$key}{delete}; } my (@output, @output1); for my $i (0 .. $#array) { if (exists $hash{ $array[$i] } and $array1[$i] == $hash{ $array[$i] }{highest}) { push @output, $array[$i]; push @output1, $array1[$i]; } } print "@output\n@output1\n";

Updated : Fixed a bug, added 7 to show the problem (the previous version did not delete 7 because there was a lesser value).

لսႽ ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ


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 AnomalousMonk (Monsignor) on Sep 25, 2013 at 22:59 UTC
    ... what I made of it ...

    My congratulations and a ++ if you made anything at all of it. I could make nothing whatsoever, especially of the  , , and  , , , bits in the output array examples.

Re^2: How do I remove the duplicates from the array?
by PetreAdi (Novice) on Sep 26, 2013 at 06:23 UTC

    Very good

    But i need

    @output = ( -20, , ,1, , , ,9,3, ,-4, 5, ,20); @output1 = ( 10, , ,9, , , ,1,3, , 5, 5, ,30); $#array==$#array1==$#output==$#output1

      In that case you can add an else block to the code generating the arrays @output and @output1 in choroba's code.

      my (@output, @output1); for my $i (0 .. $#array) { if (exists $hash{ $array[$i] } and $array1[$i] == $hash{ $array[$i] }{highest}) { push @output, $array[$i]; push @output1, $array1[$i]; } else { push @output, ''; # or use spaces ' ' here push @output1, ''; # or whatever you want } }

      Please, be more precise. Even if Perl can "do what you mean", sometimes you have to be careful.
      @long = (1, 2, 3, 4, 5); @sparse = (1, , , 4, ); @short = (1, 4); print "Equals\n" if $#long == $#sparse; print "Shorter\n" if $#short == $#sparse;
      لսႽ ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ

        Update: Never mind the below for farang had already showed that above.

        I /GUESS/ OP has a issue of keeping track of missing values and display of them. So, undef (ideally; else nothing, '') could be used for the missing values. Format the display with whitespace as needed.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (6)
As of 2014-07-31 23:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (255 votes), past polls