Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Re: looping through an array

by reasonablekeith (Deacon)
on Apr 25, 2006 at 13:00 UTC ( [id://545552]=note: print w/replies, xml ) Need Help??


in reply to looping through an array

Well, this is how I'd do it, but it's a bit lazy really. I normally like to self document, and joining the three extracted variables into one string isn't too descriptive. That said it works like this.

There are two stages, firstly going through the data and building a hash, the keys of which are all the unique combination of src_ip, dest_ip, and port. This key's value is incremented each time it's found, and so the value of the hash key forms our count value.

Then it's just a case of going through the keys, spliting them apart so we can join them in the correct format, and printing the count value.

If you were going to do anything more complicated than this then you should probably store your data in a hash of hash format, rather than my mangled string example, but I had to leave you with something to do :)

anyway, here's the code...

my $delim = "DELIM"; my %access_count; while (<DATA>) { my $access_string = join($delim, ($_ =~ m/(\d+\.\d+\.\d+\.\d+).*?( +\d+\.\d+\.\d+\.\d+)\s(\d+)/)); print "$access_string\n"; $access_count{$access_string}++; } foreach my $access_string (sort keys %access_count) { print join(' - ', (split /$delim/, $access_string), "[$access_count{$access_string}]" ) . "\n"; } __DATA__ %PIX-4-106023 Deny udp src inside 1.1.1.1 1161 dst outside 3.3.3.3 53 +by access-group inside_access_in %PIX-4-106023 Deny tcp src inside 1.1.1.1 1637 dst outside 4.4.4.4 80 +by access-group inside_access_in %PIX-4-106023 Deny tcp src inside 1.1.1.1 2519 dst outside 4.4.4.4 80 +by access-group inside_access_in %PIX-4-106023 Deny udp src inside 1.1.1.1 1161 dst outside 7.7.7.7 53 +by access-group inside_access_in %PIX-4-106023 Deny tcp src inside 1.1.1.1 2519 dst outside 9.9.9.9 80 +by access-group inside_access_in %PIX-4-106023 Deny udp src inside 1.1.1.1 1161 dst outside 9.9.9.9 443 + by access-group inside_access_in
---
my name's not Keith, and I'm not reasonable.

Replies are listed 'Best First'.
Re^2: looping through an array
by reasonablekeith (Deacon) on Apr 25, 2006 at 15:34 UTC
    You could get golfy about it too*. (using McDarren's split)
    #!/usr/bin/perl use warnings; use strict; while(<DATA>){$a->{join' ',(split)[5,9,10]}++}print join' - ',split,$a +->{$_}."\n"for sort keys%$a;

    * sorry, I know this isn't particularly useful, but I'm a bit bored here.

    ---
    my name's not Keith, and I'm not reasonable.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://545552]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (8)
As of 2024-03-28 09:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found