MAC Address Comparison

D

doni

I am trying to search and compare a given mac address with a list of
mac addresses from a file. All the MAC address values are listed in the
first field of each line.
I have tried all the combinations for comparison operation but I am not
getting the desired result.
Here are the ones that I tried where $match variable contains the given
mac address. The MAC address is available in this format:
xx:xx:xx:xx:xx:xx

1. if ($words[0] eq $match)
2. if ($words[0] =~ m/\x$match/)
3. if ($words[0] =~ m/^[$match]/)

Can anyone let me know how should I do this.

Thanks,
sekar
 
J

J. Gleixner

doni said:
I am trying to search and compare a given mac address with a list of
mac addresses from a file. All the MAC address values are listed in the
first field of each line.
I have tried all the combinations for comparison operation but I am not
getting the desired result.
Here are the ones that I tried where $match variable contains the given
mac address. The MAC address is available in this format:
xx:xx:xx:xx:xx:xx

1. if ($words[0] eq $match)
2. if ($words[0] =~ m/\x$match/)
3. if ($words[0] =~ m/^[$match]/)

Can anyone let me know how should I do this.

Yes. Post your code and someone might be able to help. Posting one
line doesn't help you and only wastes everyone else's time. We have
no idea what's in $words[0], or $match.
 
D

doni

thanks, Gleixner. Here is the code I wrote...

#! /usr/bin/perl -w

$ex_data = "stat.txt";
open (DATA,$ex_data) || die("Cannot Open File");

### Prompt the User for Input ###
print "Enter the MAC Address in this format: xx:xx:xx:xx:xx:xx";
$input = <STDIN>;

while (<DATA>) {
chomp;
@words = split;
if ($words[0] =~ m/^[$input]/) {
print "$words[0]\n";
}
}
close(DATA);

Thanks,
sekar


J. Gleixner said:
doni said:
I am trying to search and compare a given mac address with a list of
mac addresses from a file. All the MAC address values are listed in the
first field of each line.
I have tried all the combinations for comparison operation but I am not
getting the desired result.
Here are the ones that I tried where $match variable contains the given
mac address. The MAC address is available in this format:
xx:xx:xx:xx:xx:xx

1. if ($words[0] eq $match)
2. if ($words[0] =~ m/\x$match/)
3. if ($words[0] =~ m/^[$match]/)

Can anyone let me know how should I do this.

Yes. Post your code and someone might be able to help. Posting one
line doesn't help you and only wastes everyone else's time. We have
no idea what's in $words[0], or $match.
 
R

rkb

doni said:
thanks, Gleixner. Here is the code I wrote...

#! /usr/bin/perl -w

$ex_data = "stat.txt";
open (DATA,$ex_data) || die("Cannot Open File");

### Prompt the User for Input ###
print "Enter the MAC Address in this format: xx:xx:xx:xx:xx:xx";
$input = <STDIN>;

while (<DATA>) {
chomp;
@words = split;
if ($words[0] =~ m/^[$input]/) {
print "$words[0]\n";
}
}
close(DATA);

Thanks,
sekar
Several problems:
1) You need to chomp $input to remove the line ending.
2) Don't use the character class
3) You should not use DATA as your filehandle, it's the name of one of
Perl's predefined filehandles.
 
R

rkb

doni said:
thanks, Gleixner. Here is the code I wrote...

#! /usr/bin/perl -w

$ex_data = "stat.txt";
open (DATA,$ex_data) || die("Cannot Open File");

### Prompt the User for Input ###
print "Enter the MAC Address in this format: xx:xx:xx:xx:xx:xx";
$input = <STDIN>;

while (<DATA>) {
chomp;
@words = split;
if ($words[0] =~ m/^[$input]/) {
print "$words[0]\n";
}
}
close(DATA);

Thanks,
sekar
Several problems:
1) You need to chomp $input to remove the line ending.
2) Don't use the character class
3) You should not use DATA as your filehandle, it's the name of one of
Perl's predefined filehandles.

I forgot 1 other issue. You should make the regex case insensitive, or
force $input to either upper or lower case depending on what you have
in the stat.txt file.
 
D

doni

thanks sherm and rkb. I am able to get it working perfectly.

I am wondering why do I get this "Use of uninitialized value" warning
message. Can you guys let me know...

thanks,
sekar

doni said:
thanks, Gleixner. Here is the code I wrote...

#! /usr/bin/perl -w

$ex_data = "stat.txt";
open (DATA,$ex_data) || die("Cannot Open File");

### Prompt the User for Input ###
print "Enter the MAC Address in this format: xx:xx:xx:xx:xx:xx";
$input = <STDIN>;

while (<DATA>) {
chomp;
@words = split;
if ($words[0] =~ m/^[$input]/) {
print "$words[0]\n";
}
}
close(DATA);

Thanks,
sekar
Several problems:
1) You need to chomp $input to remove the line ending.
2) Don't use the character class
3) You should not use DATA as your filehandle, it's the name of one of
Perl's predefined filehandles.
 
J

John Bokma

Sherm Pendley said:
It's rare, but errors can happen with close too. Best to check for
them:

With writing, one example is that close flushes the buffer and your quota
is exceeded by the action.

Can't think up why close on read can fail atm, but I always check :)
 
R

rkb

Michele said:
while (<$data>) {
chomp;
my $address=(split)[0];
print $address, "\n" if $address eq $input;
}

As I mentioned earlier, it would be best to make the check case
insensitive. If the user enters the correct address, but the case is
different than what's in the file, the test will fail. Also, I'd exit
out of the loop if there is a match.

while (<$data>) {
chomp;
my $address=(split)[0];
print "$address\n" and last if $address =~ /^\Q$input\E$/i;
}
 
J

John Bokma

Sherm Pendley said:
Same here, on both counts. I always figure it's better safe than
sorry, when it comes to error checking.

Yup, very true. I bumped into the quota issue a few years back (or better:
a customer did :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,059
Latest member
cryptoseoagencies

Latest Threads

Top