matching elements in an array

I have an array in which the elements contain mac address.  I would like to do a element comparsion to see
whether I have duplicate mac addresses entries does anyway have the syntax to do this.

thanks in advance

sma302
sma3092Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ITcrowCommented:

sub returnUnique {
   my %uniq;
   $uniq{$_} = 1 foreach( @_ );
   return (keys %uniq);
}

@objects = qw/ a b a c d c b hello there hello here 192.168 29.1 192.168.2 192.168/;
@objects = returnUnique( @objects );
0
sma3092Author Commented:
Not sure whether this will work in my situation I have the following code:

 foreach $line (@raw_data)
               {
                  if ($line =~ /((?:[0-9a-f]{2}[:-]){5}[0-9a-f]{2})/i){     # This line searches the $line variable for  a mac address
                  $line =~ s/.*,(.*)\)$/$1/g;                                       # The mac address is stripped out from the variable which also contains surronding text
                  print "pblade $file is attached to $switch and the mac address is $line\n";
                  }
                  }

             }

            What I would like to do is store the stripped out mac address in an array then search the array for matching elements if there is a match then
            say matching mac addresses warning or there are no matching mac address...

0
ITcrowCommented:
Exactly!

sub returnUnique {
   my %uniq;
   $uniq{$_} = 1 foreach( @_ );
   return \%uniq;
}

# Let's consider an array, you can have IP or MAC or text strings or whatever you like.
@objects = qw/
192.168.0.1
192.168.0.2
172.16.1.5
192.168.0.1
/;

# Turn the listing into unique objects hash
my(%unique_objects) = %{ returnUnique( @objects ) };

# Check for matches.
$some_item = "172.16.1.5";
if( $unique_objects{ $some_item } ) {
  print "Item $some_item is present in collection\n";
}
else {
  print "Sorry! No match for $some_item.\n";
}

# To print the entire collection:
print "$_\n" foreach( keys %unique_objects);
0
Cloud Class® Course: MCSA MCSE Windows Server 2012

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

sma3092Author Commented:
Please be patient with me I am new to Perl but keen to learn more.. If I understand correctly you have created an array removed the duplicates with the returnUnique function then use a variable with a hardcoded value to check for matches.

I have an array with strings containing mac addresses.  
The mac address has been stripped from the string and is currently stored in a holding variable called $line.
I want to create an array and fill it with the mac address which are contained within $line.
Once I have done this, I want check the array to see whether any of the element match (mac address).

foreach $line (@raw_data)
               {
                  if ($line =~ /((?:[0-9a-f]{2}[:-]){5}[0-9a-f]{2})/i){     # This line searches the $line variable for  a mac address
                  $line =~ s/.*,(.*)\)$/$1/g;                                       # The mac address is stripped out from the variable which also contains surronding text
                  print "pblade $file is attached to $switch and the mac address is $line\n";
                  }
                  }

             }

Could you expand my program to do this?

thanks in advance

0
ITcrowCommented:

#-- Populate
my %macs;
foreach $line (@raw_data) {
  if($line =~ /((?:[0-9a-f]{2}[:-]){5}[0-9a-f]{2})/i) {  # This line ...
    $line =~ s/.*,(.*)\)$/$1/g;    # The mac address is stripped ....
    print "pblade $file is attached to $switch and the mac address is $line\n";
    $macs{ $line } = 1;
  }
}

#-- Check.
my(@check_mac) = qw/
mac1
mac2
mac3
/;

foreach $mac (@check_mac) {
  if( $macs{ $mac } ) {
    print "MAC $mac found in database.\n";
  }
  else {
    print "MAC $mac has NO match in database.\n";
  }
}
0
ozoCommented:
#it sounds like what sma3092 wants to do is
my %macs;
foreach $line (@raw_data) {
  if($line =~ /((?:[0-9a-f]{2}[:-]){5}[0-9a-f]{2})/i) {  # This line ...
    $line =~ s/.*,(.*)\)$/$1/g;    # The mac address is stripped ....
    print "pblade $file is attached to $switch and the mac address is $line\n";
    print "duplicate $line\n" if $macs{ $line }++;
  }
}
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
sma3092Author Commented:
hello ozo

I am pretty new to perl could you please describe what your extra pieces of code adds to my program..

thanks in advance!

sma3092

0
ITcrowCommented:
ozo,

I shouldn't matter. It is all taken care in the first paragraph itself.
Duplicate keys are seemlessly garbled by the hash table '%macs'
It will be a redundant message, as it has not impact on actual
MAC collection.

In the second paragraph, I was demonstrating that if user gets a
set of MAC addresses from elsewhere ( @check_mac) then how
each entry can be validated as present or absent from the MAC
collection.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Perl

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.