CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
Counting the number of times a sentence occurs

 



cspctec
New User

Nov 13, 2012, 7:57 AM

Post #1 of 3 (1617 views)
Counting the number of times a sentence occurs Can't Post

Hi! I have an array that contains several hundred different sentences.

For example:

User logged in successfully at 10:35:33
User logged in successfully at 10:35:34
User logged in successfully at 10:35:35
Error: Login failure!
User logged in successfully at 10:35:37
Error: Login failure!
Error: System failure at 01:23:22
Error: System failure at 01:23:40
User logged in successfully at 10:35:39
Error: System failure at 01:23:41

So... what I've been trying to do is create a perl script to tell me how many times each error occurred.

I would need the output to look like:

User logged in successfully at 10:35:33
The above event occurred 4 additional times

Error: Login failure!
The above event occurred 1 additional times

Error: System failure at 01:23:22
The above event occurred 2 additional times

I have been trying to figure out a way to make the script count each event individually, but the for loops I have been using are only counting the first few matches.

Can someone come up with an algorithm that can count these and output the results like I have listed? I'm probably just over-thinking it. Thanks for the help!


Laurent_R
Veteran / Moderator

Nov 13, 2012, 10:26 AM

Post #2 of 3 (1609 views)
Re: [cspctec] Counting the number of times a sentence occurs [In reply to] Can't Post

If you have:

User logged in successfully at 10:35:33
User logged in successfully at 10:35:34
User logged in successfully at 10:35:35

and compare the full lines, they will come up as different sentences, because the time stamp is different. So the first thing you need to do is to figure out what part of the sentence is common to all of them. In this case, it would be, for example, "User logged in successfully at ". This means that you have first to process your sentence to keep the common part.

Once you have done that, store your sentences in a hash, with the sentence being the key, and the number of occurrence the value. At the end, just scan your hash to prepare your report.

The best would be that you show us the script you have written, so that we can point to possible errors and help you correct them.


FishMonger
Veteran / Moderator

Nov 13, 2012, 10:28 AM

Post #3 of 3 (1608 views)
Re: [Laurent_R] Counting the number of times a sentence occurs [In reply to] Can't Post

Cross posted on perlmonks

http://perlmonks.org/?node_id=1003664

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives