Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7591853
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T20:45:17+00:00 2026-05-30T20:45:17+00:00

Updated: After my initial post and responses, I managed to have another crack and

  • 0

Updated:

After my initial post and responses, I managed to have another crack and have written out my aims and results a bit clearer:

Aim:

I’m attempting to count the number of hits in a search string of a log file to figure out how many occurrences of a message are generated in the following ways:

  • Total per day.
  • Total per hour.
  • Highest per min, per hour.
  • Highest per sec, per hour.

My working code:

#!/usr/bin/perl
#use strict;
use warnings;
use Data::Dumper;

my @a =  (  
    [ qw /2012-02-21_09:43:43/ ],
    [ qw /2012-02-21_09:43:43/ ],
    [ qw /2012-02-21_09:43:44/ ],
    [ qw /2012-02-21_09:43:44/ ],
    [ qw /2012-02-21_09:43:44/ ],
    [ qw /2012-02-21_09:43:45/ ],
    [ qw /2012-02-21_09:43:45/ ],
    [ qw /2012-02-21_09:43:45/ ],
    [ qw /2012-02-21_09:43:45/ ],
    [ qw /2012-02-21_09:44:47/ ],
    [ qw /2012-02-21_09:44:47/ ],
    [ qw /2012-02-22_09:44:49/ ],
    [ qw /2012-02-21_10:44:49/ ]
);

my ( %count, $count ) = ();

foreach (@a) {
    my $line = @$_[0] ;
    $line =~ /(\S+)_(\d+):(\d+):(\d+)/ ;

    my $day = $1;
    my $hour= $2;
    my $min = $3;
    my $sec = $4;

    $count {$day}->{$hour}->{$min}->{$sec}{'sec'} += 1 ;
    $count {$day}->{$hour}->{$min}{'min'} += 1 ;
    $count {$day}->{$hour}{'hour'} += 1 ;
    $count {$day}{'day'}  += 1 ;
}

#print Dumper (%count) . "\n";

foreach my $k1 ( sort keys %count ) {
    print "$k1\t$count{$k1}{'day'}\n" ;

    foreach my $k2 ( sort keys %{$count{$k1}} ) {
        if ($k2 =~ /day/) {
            next;
        }
        print " $k2:00\t\t$count{$k1}{$k2}->{'hour'}\n";

        foreach my $k3 ( sort keys %{$count{$k1}{$k2}} ) {
            if ($k3 =~ /hour/) {
                next;
            }
            print "  $k2:$k3\t\t$count{$k1}{$k2}{$k3}->{'min'}\n";

            foreach my $k4 ( sort keys %{$count{$k1}{$k2}{$k3}} ) {
                if ($k4 =~ /min/) {
                    next;
                }
                print "   $k2:$k3:$k4\t$count{$k1}{$k2}{$k3}{$k4}->{'sec'}\n";              
            }
            print "\n";
        }
        print "\n";
    }
}
exit;

Results

I’ve had to turn off strict (of which I am ashamed), due to my poor hash dereference methods.

2012-02-21  12
 09:00      11
  09:43     9
   09:43:43 2
   09:43:44 3
   09:43:45 4

  09:44     2
   09:44:47 2

 10:00      1
  10:44     1
   10:44:49 1

Attempting to output:

2012-02-21  12
 09:00      11
  09:43     9
   09:43:45 4   

 10:00      1
  10:44     1
   10:44:49 1

Questions:

  1. Is there a better way of writing the code, and turning on strict?
  2. How would I go about listing the highest occurrence of a hash value within a hash, in an attempt to list the highest number count only?

Thanks for all the previous posts, I couldn’t have gotten this far without them.

Cheers,

Andy

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T20:45:19+00:00Added an answer on May 30, 2026 at 8:45 pm

    It can be simplified somewhat (I also made some stylistic changes to improve readability):

    my @data =  (
        [ qw /2012-02-21_09:43:43/ ],
        [ qw /2012-02-21_09:43:43/ ]
    );
    my %counts;   
    foreach my $words (@data) {
        my ($day, $hour) = ($words->[0] =~ /(\d{4}-\d{2}-\d{2})_(\d+):/ );
        $counts{$day}->{$hour} += 1;
    }
    foreach my $day (keys %counts) {
        foreach my $hour (keys %{ $counts{$day} }) { 
            print "Hour count for $day:$hour is: $counts{$day}->{$hour}\n";
        }
    }
    

    The working part of the loop that is central to your query is this:

        my ($day, $hour) = ($words->[0] =~ /(\d{4}-\d{2}-\d{2})_(\d+):/ );
    
        # You don't need minutes/seconds, so don't match them
        # On the other hand, it's better to match YYYY/MM/DD explicitly!
        # A regexp match in a list context will return a list of captures! 
        #     e.g. ($1, $2, ...)
    
        $counts{$day}->{$hour} += 1;
        # You need to merely add 1 to a value. No need to push ones on a list.
    
        # Please note that if the data is not guaranteed to be perfectly formatted, 
        # you need to defend against non-matches:
        $counts{$day}->{$hour} += 1 if (defined $day && defined $hour);
    

    Here’s the same code with comments added clarifying why I made the stylistic changes:

    my @data =  (  # Don't use @a - variable name should have meanings
        [ qw /2012-02-21_09:43:43/ ], # Not sure why you are using an array ref with
        [ qw /2012-02-21_09:43:43/ ], #   just 1 element, but let's pretend that is OK
    );
    my %counts;   
    foreach my $words (@data) { # Almost never rely on $_ - less readable
        my ($day, $hour) = ($words->[0] =~ /(\d{4}-\d{2}-\d{2})_(\d+):/ ;
        $counts{$day}->{$hour} += 1; # You can omit "->" but that's less readable
    }
    foreach my $day (keys %counts) { # Always localize your variable to the block they need
        foreach my $hour (keys %{ $counts{$day} }) { 
            print "Hour count for $day:$hour is: $counts{$day}->{$hour}\n";
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have updated to latest Django version 1.0.2 after uninstalling my old Django version.But
I have a table which is updated with ajax and after update it if
I have a listview which has its datasource changed after update of a search
Partials in XML builder are proving to be non-trivial. After some initial Google searching,
I have a jQuery UI Dialog box, that I'm creating. It starts out as
(NOTE: I've updated this question from the initial inquiry about child containers towards creating
I have a T4 that is generating multiple .html files. After creating them all
I have my own answer to this question, which I'll post, but I wanted
Struggling still with this after hours or research.. I have a simple helper class
I have a django view that looks like... def add_user(request): if User.objects.get(username__exact = request.POST['username']):

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.