Hello Monks, I Have just finished reading the Llama, and started a project to hone my Perl skills. My problem question in the following code is that my regexp is not matching exactly a line, It just matches every logfile line that starts the same way, so it gives me wrong results. I wonder if it has to do with the (.*) line starter. Also, I would also like to know what do you think of my "skeleton_player" hash copying. Thank you for your time. Logfile Example
Logfile Example "ct1<4><BOT><>" entered the game "ct1<4><BOT><>" joined team "CT" "terror1<1><BOT><TERRORIST>" attacked "ct1<4><BOT><CT>" with "p228" (d +amage "26") (damage_armor "0") (health "74") (armor "0") "terror1<1><BOT><TERRORIST>" attacked "ct1<4><BOT><CT>" with "p228" (d +amage "27") (damage_armor "0") (health "47") (armor "0") "terror1<1><BOT><TERRORIST>" attacked "ct1<4><BOT><CT>" with "p228" (d +amage "27") (damage_armor "0") (health "20") (armor "0") "terror1<1><BOT><TERRORIST>" attacked "ct1<4><BOT><CT>" with "p228" (d +amage "28") (damage_armor "0") (health "-8") (armor "0") "terror1<1><BOT><TERRORIST>" killed "ct1<4><BOT><CT>" with "p228" "ct1<4><BOT><CT>" attacked "terror2<2><BOT><TERRORIST>" with "fiveseve +n" (damage "12") (damage_armor "2") (health "88") (armor "98") "ct1<4><BOT><CT>" attacked "terror3<3><BOT><TERRORIST>" with "fiveseve +n" (damage "13") (damage_armor "0") (health "2") (armor "0")
This is the code that matches wrongly.
# kill / killed logging # $1 = killer # $2 = killed # $3 = weapon used if($_ =~ m/^"(.+)<.+><.+><.+>" killed "(.+)<.+><.+><.+>" with "(.+ +)"/){ $players{$1}{"frags"} += 1; $players{$2}{"deaths"} += 1; }
As you may see in the logfile example player named "ct1" didn't killed anyone, but It's name appears 4 times as line starter, which my regular expressions treats as 4 kills. Is the error in starting my regexp with (.*)?. If it's not too much to ask I would also like to know what do think of this part, is it correct to just copy the hash?
# The skeleton of each player hash my %skeleton_player = ( "team_side" => "0", "frags" => "0", "deaths" => "0", "suicides" => "0", "dmg_done" => "0", "dmg_received" => "0", "team_damage" => "0", "team_kills" => "0", ); my %players = (); my @file = <>; foreach(@file){ # Deletes the time information from each log line $_ =~ s/L [0-9]+\/[0-9]+\/[0-9]{4} - [0-9]+\:[0-9]+\:[0-9]+\: //g; # Creates a hash of a hash for players # # And Copies %skeleton_player to it # # %players # - %player 1 # -frags # -deaths # -etc.. # - %player 2 # -etc.. if($_ =~ m/"(.*)<.*><.*><.*>" entered the game/){ unless(exists $players{$1}){ $players{$1} = %skeleton_player; } }
Thank you for taking the time to read and think about it.

In reply to Question regarding exact regexp matching by Ekimino

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.