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


In reply to Data Structures Issue by Anonymous Monk

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.