Removing Duplicate Elements in an Array 
Author Message
 Removing Duplicate Elements in an Array

I am having some issues with the removal of duplicate elements in an array.
Could someone please assist with this.  When trying to print the results, it
does not remove the duplicate.

Geoff

foreach $NoAssoc(keys %$response)
 {
  $var_assoc=$response->{$NoAssoc};


  $prev = "not equal to $array[0]";


   }



Tue, 31 Aug 2004 07:05:21 GMT  
 Removing Duplicate Elements in an Array

Quote:

> I am having some issues with the removal of duplicate elements in an array.
> Could someone please assist with this.  When trying to print the results, it
> does not remove the duplicate.

You seem to have looked at the code suggested when you say

  perldoc -q duplicate

but missed a little bit of how perl deals with references

Quote:
> Geoff

> foreach $NoAssoc(keys %$response)
>  {
>   $var_assoc=$response->{$NoAssoc};





Quote:

>   $prev = "not equal to $array[0]";



And then you uniqify this single element list each time around the loop.

Quote:
>    }

Maybe


  $prev = "not equal to $array[0]";

is what you were after, but as you have a hash you can do something like

  undef %saw;
  %saw = reverse %$response;

with the appropriate use of my to restrict the scope of %saw...

Hope this helps,

Mike

--

http://www.stok.co.uk/~mike/       | GPG PGP Key      1024D/059913DA

http://www.starnix.com/            |                  75D2 9EC4 C1C0 0599 13DA



Tue, 31 Aug 2004 09:39:38 GMT  
 Removing Duplicate Elements in an Array

Quote:

> I am having some issues with the removal of duplicate elements in an array.
> Could someone please assist with this.  When trying to print the results, it
> does not remove the duplicate.

> Geoff

> foreach $NoAssoc(keys %$response)
>  {
>   $var_assoc=$response->{$NoAssoc};


>   $prev = "not equal to $array[0]";


>    }


$previous = 'nope';


   print "$uniqSorted";

- Show quoted text -

Quote:
}



Tue, 31 Aug 2004 12:37:45 GMT  
 Removing Duplicate Elements in an Array
via the perl cookbook



hth,
mike

Quote:

> I am having some issues with the removal of duplicate elements in an array.
> Could someone please assist with this.  When trying to print the results, it
> does not remove the duplicate.

> Geoff

> foreach $NoAssoc(keys %$response)
>  {
>   $var_assoc=$response->{$NoAssoc};


>   $prev = "not equal to $array[0]";


>    }



Tue, 31 Aug 2004 13:26:01 GMT  
 Removing Duplicate Elements in an Array

Quote:
> I am having some issues with the removal of duplicate elements in an array.


This, however, will not retain the array order.



Tue, 31 Aug 2004 14:27:20 GMT  
 Removing Duplicate Elements in an Array
Mike,

Thank you for assistance, but I am either doing something wrong or I am
trying to do something else.  What I would like to see is as it goes through
this loop it builds another array.  Then I have one array to work from.

Geoff


Quote:

> > I am having some issues with the removal of duplicate elements in an
array.
> > Could someone please assist with this.  When trying to print the
results, it
> > does not remove the duplicate.

> You seem to have looked at the code suggested when you say

>   perldoc -q duplicate

> but missed a little bit of how perl deals with references

> > Geoff

> > foreach $NoAssoc(keys %$response)
> >  {
> >   $var_assoc=$response->{$NoAssoc};




with)


> >   $prev = "not equal to $array[0]";


> And then you uniqify this single element list each time around the loop.

> >    }

> Maybe


>   $prev = "not equal to $array[0]";

> is what you were after, but as you have a hash you can do something like

>   undef %saw;
>   %saw = reverse %$response;

> with the appropriate use of my to restrict the scope of %saw...

> Hope this helps,

> Mike

> --

apply.
> http://www.stok.co.uk/~mike/       | GPG PGP Key      1024D/059913DA

3D60
> http://www.starnix.com/            |                  75D2 9EC4 C1C0 0599
13DA



Tue, 31 Aug 2004 23:10:26 GMT  
 Removing Duplicate Elements in an Array

Quote:

> Thank you for assistance, but I am either doing something wrong or I am
> trying to do something else.  What I would like to see is as it goes through
> this loop it builds another array.  Then I have one array to work from.

From your original code:

Quote:
>> > foreach $NoAssoc(keys %$response)
>> >  {
>> >   $var_assoc=$response->{$NoAssoc};


I assume that you are looking for the unique values in the hash refered to by
$response.


single elements, you need to build the list then sort and uniquify it. If
your values are just strings then you might consider the other aproach in
the code below - if the values aren't just strings then does the 'ne'
comparison in your original code do what you want?

The get_unique_values2 will return stringified representations of the original
which may not be what you want.

Hope this helps,

Mike

#!/usr/bin/env perl

use strict;
use warnings;

my $r = { 'k1' => 'v1', 'k2' => 'v2', 'k3' => 'v1' };



sub get_unique_values {


    foreach my $key (keys %$response) {
        my $value = $response->{$key};
        print "adding $value to array\n";

    }





    my $prev = "not equal to $array[0]";


Quote:
}

sub get_unique_values2 {

    my %uniq = reverse %$response;
    return sort keys %uniq;

Quote:
}

--

http://www.stok.co.uk/~mike/       | GPG PGP Key      1024D/059913DA

http://www.starnix.com/            |                  75D2 9EC4 C1C0 0599 13DA


Tue, 31 Aug 2004 23:51:14 GMT  
 Removing Duplicate Elements in an Array
Mike,

I want to thank you for all of your help today.  I was finally able to
resolve my issue.  Here is the final results.

#!/usr/bin/perl -w

use strict;
use Net::SNMP;


my $site='100';

open (FaySites, "sites") or dienice("Can't open sites: $! \n");

  close(FaySites);


  $found = 0;
  {

    chomp($i);
    ($tower,$sector,$mac,$ip) = split(/\ /,$i);
    if ($tower =~ $site) {
     $found = 1;

     #print "$sector - $mac\n";

    }

 ($session, $error) = Net::SNMP->session(
      -hostname  => $ip,
      -community => 'wtnoc',
   -port      => 161
   );

   if (!defined($session)) {
      printf("ERROR: %s.\n", $error);
      next;
   }

   my $NoAssoc = '1.3.6.1.4.1.710.3.2.3.2.2.1.1.1';

   $response = $session->get_table($NoAssoc);

 if (!defined($response)) {
      printf("ERROR: %s.\n", $session->error());
      $session->close();
      next;
   }

    $session->close();

 foreach $NoAssoc(keys %$response)
 {
  $var_assoc=$response->{$NoAssoc};

 }
  }

Quote:
}




###Sub Routines Start Here###
sub dienice {

        print "Error\n";
        print $msg;
        exit;

Quote:
}

Thank you again - to everyone,

Geoff


Quote:

> > Thank you for assistance, but I am either doing something wrong or I am
> > trying to do something else.  What I would like to see is as it goes
through
> > this loop it builds another array.  Then I have one array to work from.

> From your original code:

> >> > foreach $NoAssoc(keys %$response)
> >> >  {
> >> >   $var_assoc=$response->{$NoAssoc};

> I assume that you are looking for the unique values in the hash refered to
by
> $response.


different
> single elements, you need to build the list then sort and uniquify it. If
> your values are just strings then you might consider the other aproach in
> the code below - if the values aren't just strings then does the 'ne'
> comparison in your original code do what you want?

> The get_unique_values2 will return stringified representations of the
original
> which may not be what you want.

> Hope this helps,

> Mike

> #!/usr/bin/env perl

> use strict;
> use warnings;

> my $r = { 'k1' => 'v1', 'k2' => 'v2', 'k3' => 'v1' };




> sub get_unique_values {


>     foreach my $key (keys %$response) {
>         my $value = $response->{$key};
>         print "adding $value to array\n";

>     }





>     my $prev = "not equal to $array[0]";


> }

> sub get_unique_values2 {

>     my %uniq = reverse %$response;
>     return sort keys %uniq;
> }

> --

apply.
> http://www.stok.co.uk/~mike/       | GPG PGP Key      1024D/059913DA

3D60
> http://www.starnix.com/            |                  75D2 9EC4 C1C0 0599
13DA



Wed, 01 Sep 2004 05:46:34 GMT  
 
 [ 8 post ] 

 Relevant Pages 

1. removing duplicates from an array

2. duplicate checker and remove from array?

3. Best way of removing duplicate array entries

4. Removing duplicates from an array

5. removing duplicate values from array not working...

6. removing duplicates from an array of hashes

7. Arrays help - Removing Duplicate entries.

8. Not inserting duplicate elements into an array

9. Finding duplicate elements in an array?

10. Removing duplicates from an array

11. Duplicate elements in arrays

12. eliminate *mostly* duplicate array elements

 

 
Powered by phpBB® Forum Software