in reply to To remove duplicate value from an array
As someone once said, "You can program C in any language."
Here is a more Perl-ish solution:
#!/usr/bin/perl -w use strict; use warnings 'all'; my @arr = ( 1, 2, 3, 1 ); my %saw = ( ); my @unique = grep { ! $saw{$_}++ } @arr; use Data::Dumper; warn Dumper( \@unique );
That one line...
my @unique = grep { ! $saw{$_}++ } @arr;
...is where all the action is.
We create a hash (%saw) and the only items that make it into @unique are elements that have not been filtered through grep { $saw{$_}++ }.
Kudos to you though for really thinking through this problem. Just goes to show - There's More Than One Way To Do It!
|
|---|