Re: How do I count & skip the number of duplicate elements in an array

2006-12-12 Thread Peter Scott
On Mon, 11 Dec 2006 06:02:25 -0800, Rinku Mahesh wrote:
> -
>   my @fields =();
>   my @unique_elements=();
>   my %seen   = ();
>
>
>   foreach my $elem (@fields) {
>   next if $seen{ $elem }++;
>   push (@unique_elements, $elem);
> }
> -
>
>   What needs to be done to compute the count of skipped/duplicate elements ?

print "$_ seen $seen{$_} times\n" for grep { $seen{$_} > 1 } keys %seen;

-- 
Peter Scott
http://www.perlmedic.com/
http://www.perldebugged.com/


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
 




Re: How do I count & skip the number of duplicate elements in an array

2006-12-11 Thread John W. Krahn
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]
 




How do I count & skip the number of duplicate elements in an array

2006-12-11 Thread Rinku Mahesh
Hi,
   
 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   = ();
   
   
  foreach my $elem (@fields) {
  next if $seen{ $elem }++;
  push (@unique_elements, $elem);
}
-
   
  What needs to be done to compute the count of skipped/duplicate elements ?
   
  Thanks,
   
  Rinku

 
-
Access over 1 million songs - Yahoo! Music Unlimited.