Feb 16, 2004 #1 bretttt Programmer Jul 23, 2002 74 US Hello, I have an array which contains example: 1,3,5,7,5,3,9,10,11,12 how can i remove any duplicates such as the threes and fives? if (@array){ foreach(@array){ } } thanks
Hello, I have an array which contains example: 1,3,5,7,5,3,9,10,11,12 how can i remove any duplicates such as the threes and fives? if (@array){ foreach(@array){ } } thanks
Feb 16, 2004 #2 Deleted Technical User Jul 17, 2003 470 US sub remove_duplicates { my @array = @_; # Remove duplicates my %temp_hash; $temp_hash{$_} = 1 for (@array); my @new_array = (); push @new_array, $_ for (keys %temp_hash); return @new_array; } M. Brooks X Concepts LLC http://xconcepts.com Upvote 0 Downvote
sub remove_duplicates { my @array = @_; # Remove duplicates my %temp_hash; $temp_hash{$_} = 1 for (@array); my @new_array = (); push @new_array, $_ for (keys %temp_hash); return @new_array; } M. Brooks X Concepts LLC http://xconcepts.com
Feb 16, 2004 #3 icrf Programmer Dec 4, 2001 1,300 US Same idea, but shorter done with a map: Code: my @array = (1,3,5,7,5,3,9,10,11,12); my %hash = map {$_ => 1} @array; my @uniq = keys %hash; ________________________________________ Andrew - Perl Monkey Upvote 0 Downvote
Same idea, but shorter done with a map: Code: my @array = (1,3,5,7,5,3,9,10,11,12); my %hash = map {$_ => 1} @array; my @uniq = keys %hash; ________________________________________ Andrew - Perl Monkey
Feb 16, 2004 #4 rosenk Programmer Jan 16, 2003 403 I like the perl cookbook method: Code: my @array = (1,3,5,7,5,3,9,10,11,12); my %seen; @uniq = grep { ! $seen{$_}++ } @array; Upvote 0 Downvote
I like the perl cookbook method: Code: my @array = (1,3,5,7,5,3,9,10,11,12); my %seen; @uniq = grep { ! $seen{$_}++ } @array;
Feb 16, 2004 #5 rosenk Programmer Jan 16, 2003 403 (one problem with the other two methods shown, by the way, is that order of the original array is lost.) Upvote 0 Downvote
(one problem with the other two methods shown, by the way, is that order of the original array is lost.)