Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I've got a small issue on my hands. I have been attempting to put together a small log-parsing module for the popular on-line game Counter-Strike. Its working well, except for one part. In the hash %TYPES, I have the values being anon. arrays. The second element could contain a sub reference or a list of sub references. When the log is parsed, the program completely does not recognize any elements pass the first with the "killed" key.

So, the code below should bark back out that it doesn't know what to do with "11". It doesn't. Adding the second array value to any other line in %TYPES creates the error. Killed is the only place it does not work. This has really been bugging me and I've spent a good few hours trying to figure out why.

my %TYPES = ( killed => [qr{"(.+?)<\d+>" killed "\[.+?\] (.+?)<\d+>" with + (.+)},"11"], chat => [qr{\002(.+?) : {3,5}(.+?)\s*?$}], connected => [qr{"(.+?)<(\d+)><WON:(\d+)>" connected, address + "(.+?):(\d+?)"}], disconnected => [qr{"(.+?)<\d+>" disconnected$}], entered_game => [qr{"(.+?)<\d+>" has entered the game$}], changed_name => [qr{"(.+?)<\d+>" changed name to "(.+?)<\d+>"}] +, killed => [qr{"(.+?)<\d+>" killed "\[.+?\] (.+?)<\d+>" with + (.+)}], touched_hostage => [qr{(.+?) touched a hostage$}], planted_bomb => [qr{(.+?) planted the bomb$}], target_bombed => [qr{\*\*\*\#Target_Bombed\*\*\*$}], rcon => [qr{Rcon from "(.+?)":"\(rcon .+? (.+?) \)"$}], touched_hostage => [qr{(.+?) touched a hostage$}], rescued_hostage => [qr{(.+?) has rescued a hostage$}], killed_hostage => [qr{(.+?) killed a hostage$}], joining_team => [qr{(.+?) is joining the (.+?) force}], tk => [qr{(.+?) killed a teammate$}], ct_win => [qr{\*\*\*\#CTs_Win\*\*\*$}], t_win => [qr{\*\*\*\#Terrorists_Win\*\*\*$}], target_saved => [qr{\*\*\*\#Target_Saved\*\*\*$}], hostages_not_rescued => [qr{\*\*\*\#Hostages_Not_Rescued\*\*\*$}], round_draw => [qr{\*\*\*\#Round_Draw\*\*\*$}], killed_by_world => [qr{"(.+?)<\d+?>" killed by world with (.+?)$} +], map_cycle => [qr{Spawning server "(.+?)"}], # scores.. ); sub parse { my $self; ($self,$_) = @_; chomp($_); if(s!L ((\d\d)/(\d\d)/(\d\d\d\d) - (\d\d):(\d\d):(\d\d)): !!){ my ($month,$day,$year,$hour,$min,$sec) = ($2,$3,$4,$5,$6,$7,$8); $self->_set_time($1); if($self->{_use_epoch} == 1){ my $tmp = $month; $tmp--; $self->_set_epochtime(timelocal($sec,$min,$hour,$day,$tmp,$year) +); } } foreach my $key (keys %TYPES){ if(/$TYPES{$key}[0]/){ if(ref($self->{"_ref_$key"}) eq 'CODE'){ $self->{"_ref_$key"}->($1,$2,$3,$4,$5) } elsif(ref($self->{"_ref_$key"}) eq 'ARRAY'){ foreach my $sr (@{$self->{"_ref_$key"}}){$sr->($1,$2,$3,$4,$5) +} } elsif(ref($self->{_ref_default}) eq 'CODE'){ $self->{_ref_default}->($1,$2,$3) } else { # die "Big error"; } if(defined $TYPES{$key}[1]){ print "It is defined for $key\n"; if(ref($TYPES{$key}[1]) eq 'ARRAY'){ # if second arg is array foreach my $sr (@{$TYPES{$key}[1]}){$sr->($self,$1,$2,$3,$4, +$5)} # run each subref w/ args } # be sure to pa +ss it object ref first! elsif(ref($TYPES{$key}[1]) eq 'CODE'){ # if its just a single +ref $TYPES{$key}[1]->($self,$1,$2,$3,$4,$5); # run that. } else { print "No idea what to do with $TYPES{$key}[1] ".ref($TYPES{ +$key}[1])." ??\n"; } } last; } } }

Any help as to why the second value of the array in the "killed" value of %TYPES is not recognized would be greatly appreciated.
Thanks.

2001-03-11 Edit by Corion: Added READMORE tag

Replies are listed 'Best First'.
(tye)Re: Data Structures Issue
by tye (Sage) on Mar 11, 2001 at 09:53 UTC

    It is using the value for the last time that you used the key of "killed" (between the keys "changed_names" and "touched_hostage").

    I also suggest you follow the advice in Death to Dot Star! and replace a bunch of case of .*? and .+? with things more like [^)]* (unless those delimiter characters are allowed in those places). Your code will be faster, for one thing.

            - tye (but my friends call me "Tye")