grep

A

Anders Bystrup

Hi.

I'm trying to make the following work:

$index = 0;
foreach (@mac){
if (grep /$_/, $legalmacs !~ 0){
push @violators, join('',@mac[$index]," found on $switch port
@port[$index]\n"
$index++;
}
$index++;
}

The point is that i have a list ($legalmacs) that contains a list of mac
addresses permitted on our network. @mac contains mac addresses actually
found on the network (along with an acompanying @port of the port number
on which the mac was found). I need to check whether a @mac is in the
list $legalmacs - and if not add it and the port to @violators.
When i use the code above i simply get all the macs found...

I probably misunderstood something basic as usual - anyone with an idea to
make it work?

/Anders
 
M

Matija Papec

X-Ftn-To: Anders Bystrup

Anders Bystrup said:
Hi.

I'm trying to make the following work:

$index = 0;
foreach (@mac){
if (grep /$_/, $legalmacs !~ 0){

I'm curious how does $legalmacs looks, why are you using grep here, and why
you're using regular exp. operator on 0?
push @violators, join('',@mac[$index]," found on $switch port
@port[$index]\n"
$index++;
}
$index++;
}

Is this your actual code? You have two $index increments.
 
S

Steve Grazzini

Anders Bystrup said:
The point is that i have a list ($legalmacs) that contains a list of mac
addresses permitted on our network. @mac contains mac addresses actually
found on the network (along with an acompanying @port of the port number
on which the mac was found). I need to check whether a @mac is in the
list $legalmacs - and if not add it and the port to @violators.

I might set it up like this:

my %ok = map { $_ => 1 } qw{
00:00:00:00:00:01
00:00:00:00:00:03
};

my @bad = map {
my ($mac, $port) = split;
$ok{$mac} ? () : [$mac,$port];
} <DATA>;

for (@bad) {
printf "%s found on port %s\n", @$_;
}

__END__
00:00:00:00:00:01 $port
00:00:00:00:00:02 $port
00:00:00:00:00:03 $port
00:00:00:00:00:04 $port

The main point is: use a hash instead of repeated grepping.
 
N

Nicholas Dronen

AB> Hi.

AB> I'm trying to make the following work:

AB> $index = 0;
AB> foreach (@mac){
AB> if (grep /$_/, $legalmacs !~ 0){

I believe this evaluates to true if $_ is a substring of $legalmacs,
and $legalmacs contains a 0. That's not what you want. The second
argument to grep is a LIST, so make $legalmacs an array. See below.

AB> push @violators, join('',@mac[$index]," found on $switch port
AB> @port[$index]\n"
AB> $index++;
AB> }
AB> $index++;
AB> }


AB> The point is that i have a list ($legalmacs) that contains a list of mac
AB> addresses permitted on our network. @mac contains mac addresses actually
AB> found on the network (along with an acompanying @port of the port number
AB> on which the mac was found). I need to check whether a @mac is in the
AB> list $legalmacs - and if not add it and the port to @violators.
AB> When i use the code above i simply get all the macs found...

AB> I probably misunderstood something basic as usual - anyone with an idea to
AB> make it work?

Untested, but I think this is closer to what you want.

my @legalmacs = qw(...); # list of legal MAC addresses
my (@macs, @violators);
my %ports; # keyed by MAC address

# Populate @macs with all MAC addresses found on the network.

for my $mac (@macs) {
unless (grep /\b$mac\b/, @legalmacs) {
push @violators, $mac; # port number can be retrieved from %ports later
}
}

Regards,

Nicholas
 

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

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top