Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > grep

Reply
 
 
Anders Bystrup
Guest
Posts: n/a
 
      08-07-2003
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
 
Reply With Quote
 
 
 
 
Matija Papec
Guest
Posts: n/a
 
      08-07-2003
X-Ftn-To: Anders Bystrup

"Anders Bystrup" <> wrote:
>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.



--
Matija
 
Reply With Quote
 
 
 
 
Steve Grazzini
Guest
Posts: n/a
 
      08-07-2003
Anders Bystrup <> wrote:
> 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.

--
Steve
 
Reply With Quote
 
Nicholas Dronen
Guest
Posts: n/a
 
      08-07-2003
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Unix commands implemented ? (grep, wc, cat...) Spendius Java 2 12-13-2004 01:58 PM
Grep Hans Bijvoet Java 5 11-20-2004 01:52 AM
perl vs Unix grep Al Belden Perl 1 07-07-2004 05:58 AM
s/// has apparent side effect on grep() John E. Jardine Perl 2 04-13-2004 08:45 PM
Pattern matching help! grep emails from file! danpres2k Perl 3 08-25-2003 02:47 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57