in reply to Counting the number of times several strings appear

Hi, cspctec, and welcome to PerlMonks!

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

Can you share this script? This may help with suggestions...

Can someone come up with an algorithm that can count these and output the results like I have listed?

Sounds like a hash--where the keys store the error--may serve your purpose.

  • Comment on Re: Counting the number of times several strings appear

Replies are listed 'Best First'.
Re^2: Counting the number of times several strings appear
by cspctec (Sexton) on Nov 13, 2012 at 17:18 UTC

    Thank you for the reply

    There is really no way for me to post my entire script as it is too long and on my unix box and it cannot be connected to the internet...

    I will post some of the script here that I've been working on

    my $count = 0; $array_length = grep m/^.*\n/, @storage; #Storage array contains my fi +le.. Use grep to get the number of sentences for (my $i = 0; $i <= $array_length; $i++) { my $x = 1; if ($storage[$i] eq $storage[$x]) { $count++; } $x++; }

    That will return the number of matches of the first 12 sentences in the file... until it hits a line that is not like the first 12...

    I do not know how to make it gather a count of all of the lines that are the same and return a count for each of them

      Hi, gather a count of all of the lines that are the same
      You may have to check each of the lines, because not all the lines are the same. e.g

      User logged in successfully at 10:35:33 User logged in successfully at 10:35:34 User logged in successfully at 10:35:35 User logged in successfully at 10:35:37
      The lines above are not the same, because of the changing time the user logged in.

      If you tell me, I'll forget.
      If you show me, I'll remember.
      if you involve me, I'll understand.
      --- Author unknown to me

        Yes... sorry, those were bad examples...

        I have truncated all of the garbage that is different, such as the time the user logged in... The lines are the same in my script but I just can't figure out how to make the script keep a running total of each line that is the same as another line... I'm thinking I may have to use a LOT of variables to keep totals, or use the database suggested...