Link to home
Start Free TrialLog in
Avatar of sma3092
sma3092

asked on

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
Avatar of ITcrow
ITcrow


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 );
Avatar of sma3092

ASKER

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...

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);
Avatar of sma3092

ASKER

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


#-- 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";
  }
}
ASKER CERTIFIED SOLUTION
Avatar of ozo
ozo
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of sma3092

ASKER

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

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.