Thread Regülarer Ausdrück zu einem hash array
(19 answers)
Opened by Tom99 at 2008-10-04 15:42
Hier mal ein Beispiel
Code: (dl
)
1 2:53 ClientConnect: 0 Code (perl): (dl
)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 #!/usr/bin/perl -w use Data::Dumper; use Switch; my $file = "C:/Programme/UrbanTerror/q3ut4/games.log"; my %players; open ( FILE, $file ) or die ("Cannt open File $file"); while (defined(my $line = <FILE>)) { if ($line =~ /^\s+(\d{1,2}:\d{2}) ((\w+):(?: )?(.*)?)/) { my $time = $1; if (defined ($3)) { if (defined ($4)) { if ($3 eq "ClientConnect") { &ClientConnect($4); } if ($3 eq "ClientBegin") { &ClientBegin($4); } if ($3 eq "ClientUserinfo") { &ClientUserinfo($4, $time); } if ($3 eq "ClientUserinfoChanged") { &ClientUserinfoChanged($4); } } } } } sub ClientConnect { local $id = shift; # print ("Client $id connected\n"); return 1; } sub ClientBegin { local $id = shift; # print ("Client $id began\n"); return 1; } sub ClientUserinfo { local ($line,$time) = @_; if ($line =~ /(\d+) \\(.*)/) { #print "Client $1: $2\n"; # HIER SOLL local $id = $1; local $logline = $2; local @array = ($logline =~ /([^\\]+)\\([^\\]+)/g); local %playerinfo; for ($i = 0; $i <= @array; $i += 2) { if (defined ($array[$i]) && defined ($array[$i+1])) { $playerinfo{$array[$i]} = $array[$i+1]; } } if (defined ($playerinfo{"team"})) { if ($playerinfo{"team"} eq "free") {$playerinfo{"team"} = "0";} else if($playerinfo{"team"} eq "red") {$playerinfo{"team"} = "1";} else if ($playerinfo{"team"} eq "blue") {$playerinfo{"team"} = "2";} else {$playerinfo{"team"} = "3";} } $playerinfo{"id"} = $id; $playerinfo{"time"} = $time; $players{$id} = \%playerinfo; } return 1; } sub ClientUserinfoChanged{ local $line = shift; #print $line."\n"; if ($line =~ /(\d+) (.*)/) { local $id = $1; local $info = $2; local ($name,$team) = ($info =~ /n\\([^\\]+)\\t\\([^\\]+)/); $players{$id}{"name"} = $name; $players{$id}{"team"} = $team } return 1 } print Dumper \%players; |