[Webinar] Streamline your web hosting managementRegister Today

x
?
Solved

matching elements in an array

Posted on 2004-10-27
8
Medium Priority
?
399 Views
Last Modified: 2010-03-05
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
0
Comment
Question by:sma3092
  • 4
  • 3
8 Comments
 
LVL 5

Expert Comment

by:ITcrow
ID: 12419816

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
 

Author Comment

by:sma3092
ID: 12419972
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
 
LVL 5

Expert Comment

by:ITcrow
ID: 12420083
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
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

Author Comment

by:sma3092
ID: 12420242
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
 
LVL 5

Expert Comment

by:ITcrow
ID: 12420340

#-- 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
 
LVL 85

Accepted Solution

by:
ozo earned 500 total points
ID: 12422151
#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
 

Author Comment

by:sma3092
ID: 12422380
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
 
LVL 5

Expert Comment

by:ITcrow
ID: 12427258
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

Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
Article by: Tammy
MySQLTuner is a script written in Perl that allows you to review a MySQL installation quickly and make adjustments to increase performance and stability. The current configuration variables and status data is retrieved and presented in a brief forma…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Six Sigma Control Plans
Suggested Courses

590 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question