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.