in reply to Parsing LDAP log file given a time period

You may be better off using a hash for your code:
#!/usr/local/bin/perl -w use strict; my $processed_log = $ARGV[ 0 ]; # Now that we have the logs, let's give a breakdown of BIND/UNBINDS # per hour. To start, I'll make it easy and just do them on an hourly # basis (eg. 09:00-09:59, 10:00-10:59, etc...) open PROCESSED_LOG, '<', $processed_log or die "Cannot open $processed +_log: $!\n"; print"BINDS UNBINDS\n"; my %data; while ( <PROCESSED_LOG> ) { if ( /(\d\d):\d\d:\d\d.+?(UNBIND|BIND)/ ) { $data{ $1 }{ $2 }++; } } for my $hour ( sort keys %data ) { printf "Hour %s has %5d BINDS and %5d UNBINDS TOTAL=%-6d\n", $hou +r, @{ $data{ $hour } }{ qw/BIND UNBIND/ }, $data{ $hour }{ BIND } + $ +data{ $hour }{ UNBIND }; }
If you wanted to do it in five minute intervals then:
#!/usr/local/bin/perl -w use strict; my $processed_log = $ARGV[ 0 ]; my $minute_interval = 5; # or use command line or getopts() # Now that we have the logs, let's give a breakdown of BIND/UNBINDS # per hour. To start, I'll make it easy and just do them on an hourly # basis (eg. 09:00-09:59, 10:00-10:59, etc...) open PROCESSED_LOG, '<', $processed_log or die "Cannot open $processed +_log: $!\n"; print"BINDS UNBINDS\n"; my %data; while ( <PROCESSED_LOG> ) { if ( /(\d\d):(\d\d):\d\d.+?(UNBIND|BIND)/ ) { my $time = sprintf '%02d:%02d', $1, int( $2 / $minute_interval + ) * $minute_interval; $data{ $time }{ $3 }++; } } for my $hour ( sort keys %data ) { printf "Hour %s has %5d BINDS and %5d UNBINDS TOTAL=%-6d\n", $hou +r, @{ $data{ $hour } }{ qw/BIND UNBIND/ }, $data{ $hour }{ BIND } + $ +data{ $hour }{ UNBIND }; }

Replies are listed 'Best First'.
Re^2: Parsing LDAP log file given a time period
by spartan (Pilgrim) on Oct 20, 2006 at 18:21 UTC
    Uhmm, wow. That was brilliant. No magic here, but magnificent mathematics at work. Taking the minutes and dividing by the requested interval, and then multiplying by the interval yields the requested interval (int was instrumental, I see this now). and as for using a hash to count the number of BINDS/UNBINDS per interval is inspiring to me.

    I've often tried to incorporate hashes into programs to take advantage of it's inherent nature to group data (am I saying that right? *shrug*), but I do not use that often enough to recognize problems like this, that a hash makes easier to deal with.

    What is that skill called? I want to say data-something or other.

    Thank you for the quick reply, it works beautifully.

    Now, that's not to say I've let the rest of you off the hook, I'd still like to see how you might have tackled this problem. I'll add the above to my repertoire of code that I routinely search through to help me conquer these types of problems.

    Very funny Scotty... Now PLEASE beam down my PANTS!

      I've often tried to incorporate hashes into programs to take advantage of it's inherent nature to group data (am I saying that right? *shrug*), but I do not use that often enough to recognize problems like this, that a hash makes easier to deal with.

      What is that skill called? I want to say data-something or other.

      Data structures, perhaps? Knowing how to store your data so that it is easy to manipulate for your intended purpose is, indeed, an important programming skill — in any programming language.

      Ask yourself, <q>What do I need to be able to do with these data?</q> Let the answer drive your data structures.


      Sanity? Oh, yeah, I've got all kinds of sanity. In fact, I've developed whole new kinds of sanity. Why, I've got so much sanity it's driving me crazy.
        That's it! Data structures... My problem is I have no formal schooling in programming, unless you count basic, and pascal (circa 1988). I guess I'll have to continue learning as I've done in the past, from books and personal experience.

        Thanks for that...

        Very funny Scotty... Now PLEASE beam down my PANTS!