On Wed, Feb 20, 2008 at 11:03 PM,  <[EMAIL PROTECTED]> wrote:
snip
>  Oh - I wanted to eliminate all members of the array that had more than
>  10 instances of the same port.  I was hoping that you could do
>  something like "count keys where port = gi1/1/49".  After knowing how
>  many gi1/1/49 there were, you could remove them if they met your high
>  water mark.
>
>  I ended out just looking for duplicates, which will achieve the basic
>  task at hand.:
snip

The first thing you need to do is start using the strict* and
warning** pragmas.  You may think they are an unnecessary pain now,
but trust us, they are invaluable.   Try this:

#!/usr/bin/perl

use strict;
use warnings;

use constant ROGUE_THRESHOLD => 3;

open my $bad, ">", "probably_on_a_rogue_swtich.txt"
        or die "could not open probably_on_a_rogue_swtich.txt: $!";

open my $good, ">", "good.txt"
        or die "could not open good.txt: $!";

my %data_by_port;
while (<DATA>) {
        my ($port) = (split)[3];
        push @{$data_by_port{$port}}, $_;
}

for my $port (keys %data_by_port) {
        if ($port ne 'wireless' && @{$data_by_port{$port}} >= ROGUE_THRESHOLD) {
                for my $line (@{$data_by_port{$port}}) {
                        print $bad $line
                }
        } else {
                for my $line (@{$data_by_port{$port}}) {
                        print $good $line
                }
        }
}


__DATA__
13      switch-1        111427f2ffff    gi1/1/49
13      switch-1        111511614fff    gi1/1/49
13      switch-1        11155e45ffff    gi1/1/49
13      switch-1        1115fc4753ff    gi1/1/49
111     switch-1        11196f977f72    gi1/1/49
111     switch-1        11196fff3728    gi1/1/49
111     switch-1        11196fe74f5f    gi1/1/49
111     switch-1        111f56f1fcef    gi1/1/1
111     switch-1        111f6123f789    gi1/1/2
111     switch-1        111f6124336f    gi1/1/2
111     switch-1        111f61245f94    gi1/1/5
111     switch-1        111f6147eeff    gi1/1/2
111     switch-1        111f61896fff    gi1/1/2
111     switch-2        211f61896fff    wireless
111     switch-2        311f61896fff    wireless
111     switch-2        411f61896fff    wireless
111     switch-2        511f61896fff    wireless
111     switch-2        611f61896fff    wireless



* http://perldoc.perl.org/strict.html
** http://perldoc.perl.org/warnings.html


-- 
Chas. Owens
wonkden.net
The most important skill a programmer can have is the ability to read.

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/


Reply via email to