Rinku Mahesh wrote:
> Hi,
Hello,
> I've an array storing some numbers , out of which few are duplicate.
> I'd like to skip those which are duplicate and save them in a new
> array and same time I wish to count the number of skipped/duplicate
> elements.
>
> Eg:-
>
> Given Array "fields" {12,12,12,12,17,20,25,100,100,100}
>
> Expected Output :- Array "unique_elements" {12,17,20,25,100}
>
> Count Information based upon "Array "fields" & Array "unique_elements" :-
>
> 12 appeared 4 times
> 17 appeared once
> 20 appeared once
> 25 appeared once
> 100 appeared 4 times
>
> -----------------
> my @fields =();
> my @unique_elements=();
> my %seen = ();
$ perl -e'
my @fields = ( 12, 12, 12, 12, 17, 20, 25, 100, 100, 100 );
my %seen;
my @unique_elements = grep !$seen{ $_ }++, @fields;
foreach my $elem ( @unique_elements ) {
print "$elem appeared $seen{$elem} times\n";
}
'
12 appeared 4 times
17 appeared 1 times
20 appeared 1 times
25 appeared 1 times
100 appeared 3 times
John
--
Perl isn't a toolbox, but a small machine shop where you can special-order
certain sorts of tools at low cost and in short order. -- Larry Wall
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>