grep

Discussion in 'Perl Misc' started by Anders Bystrup, Aug 7, 2003.

  1. 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
    Anders Bystrup, Aug 7, 2003
    #1
    1. Advertising

  2. Anders Bystrup

    Matija Papec Guest

    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
    Matija Papec, Aug 7, 2003
    #2
    1. Advertising

  3. 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
    Steve Grazzini, Aug 7, 2003
    #3
  4. 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
    Nicholas Dronen, Aug 7, 2003
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. danpres2k
    Replies:
    3
    Views:
    7,467
    danpres2k
    Aug 25, 2003
  2. John E. Jardine

    s/// has apparent side effect on grep()

    John E. Jardine, Apr 12, 2004, in forum: Perl
    Replies:
    2
    Views:
    444
    John Jardine
    Apr 13, 2004
  3. Al Belden

    perl vs Unix grep

    Al Belden, Jul 3, 2004, in forum: Perl
    Replies:
    1
    Views:
    5,185
    Giridhar Nandigam
    Jul 7, 2004
  4. Hans Bijvoet

    Grep

    Hans Bijvoet, Nov 19, 2004, in forum: Java
    Replies:
    5
    Views:
    820
    David Zimmerman
    Nov 20, 2004
  5. Spendius
    Replies:
    2
    Views:
    2,958
    Rogan Dawes
    Dec 13, 2004
Loading...

Share This Page