Solved

matching elements in an array

Posted on 2004-10-27
391 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
Question by:sma3092
    8 Comments
     
    LVL 5

    Expert Comment

    by: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 );
    0
     

    Author Comment

    by:sma3092
    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
    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
     

    Author Comment

    by:sma3092
    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

    #-- 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 84

    Accepted Solution

    by:
    #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
    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
    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

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

     Java Android Coding Bundle

    Whether you're an Apple user or Android addict, learning to code for the Android platform is an extremely valuable, in-demand skill. It all starts with Java, the language behind the apps and games that make Android the top platform it is today.

    Suggested Solutions

    I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
    There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
    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…
    With the advent of Windows 10, Microsoft is pushing a Get Windows 10 icon into the notification area (system tray) of qualifying computers. There are many reasons for wanting to remove this icon. This two-part Experts Exchange video Micro Tutorial s…

    856 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

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now