Anders Bystrup <> wrote:
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
--
"Why shouldn't I top-post?"
http://www.aglami.com/tpfaq.html
"Meanings are another story."
http://www.ifas.org/wa/glossolalia.html