snafu has asked for the wisdom of the Perl Monks concerning the following question:
I am writing a script for a quake server that will allow me to keep track of the players on the server. I eventually will be writing code to allow the players to vote for maps and whatnot but I am having difficulty with this one part.
I am trying to create a hash of arrays that will allow me to keep a tab on the players such that the hash will be formed:
I think Im doing something wrong though. Let me give all of the code: <readmore>$player_info{$ipaddr} = [ qw($nickname $userid $player_req) ];
This is where I am having problems. Im using this lil block to simply check my hash of arrays.# get_player_info(): # intention: Get a listing of all the players in the server # and place that info into a global array. Return # the number of players on the server. sub player_stat() { my $funcName = (caller(0))[3]; my $raw_data = &send(1,"status"); my $watch = 0; my $pass = 0; my $num_players; my $player_name; my %player_info; my $player_nickname; my $player_userid; my $key; my $ipaddr; my @status; # Wow, I am actually starting to get this stuff # I wrote the following without asking anyone # about how to do it. Whats this do you ask? # It takes several lines of data in a single element # of an array and splits each line up into seperate # elements in the array. map { push (@status, $_) } map { split(/\n/,$_) } $raw_data; for ( @status ) { chomp; # eliminate that nasty terminator! :) $_ =~ s/\0//; # Ignore the stuff that comes from this # command for now. Maybe later I will # use this information for something but # I will wait until a more mature release # of this code. if ( $watch ) { # # name userid frags # address rate ping drop # ------------------ ---- ---- ----- # player name userid frags # IPaddr rate ping drop <- record 1 # player name userid frags # IPaddr rate ping drop <- record 2 # # This parses out just the name,userid and IP per record # so that the array is now propagated with # player_name,userid,IPaddr <- from reocrd 1 # player_name,userid,IPaddr <- from record 2 # etc. if ( $_ ne "" ) { if ( $pass == 0 ) { $pass = 1; } if ( $pass == 1 ) { $_ =~ s/\Q( .*)\E\s+([0-9]+)\s+.*// +; $player_nickname = $1; $player_userid = $2; $pass = 2; } elsif ( $pass == 2 ) { ($key = $_ ) =~ /((?:\d+\.){3}\d+)/; $player_info{$1} = [ qw($value1 $value2) ]; $pass = 1; } } } # start counting after we see the "-"'s $watch = 1 if ( /-/ ); }
block just above heremy $d = 0; my $e = 0; my @val; while ( (my $nkey, my @nvalue) = each %player_info ) { #print "Have an array (\@nvalue)\n" if wantarray(@nvalue); print "Key = $nkey and nvalue = "; $e++; for ( @nvalue ) { print "$_\n$_[0]\n$_[0]->[0]\n"; $d++; print join(", ",$_[$e]->[$d]); } print "\n";
When I run this script I get the following:} return(%player_info); }
[qadmin@concon devel]$ ./qservwatch2.pl Key = 192.168.100.10 and nvalue = Use of uninitialized value in concat +enation (.) at ./qservwatch2.pl line 522. Use of uninitialized value in concatenation (.) at ./qservwatch2.pl li +ne 522. ARRAY(0x8373ebc) Use of uninitialized value in join at ./qservwatch2.pl line 524.
I have tried everything I can find but I think I am confusing myself somehow and am just plain doing something wrong. I have been reading out of the Programming Perl (second edition) book. It has been great. In fact, I was reading in it the other day that I can use DB_File with tie() to an undef'ed file that would allow me to create a DBM in memory. Would this be a better route to go? Considering that players are not allowed to exceed 20 at a time so it wouldn't be that big as far as a memory footprint is concerned.
TIA guys. Please be gentle. Im still learning this stuff and will be for about...oh, 40 years =P
----------
- Jim
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: hash of arrays -- Im missing something
by Zaxo (Archbishop) on Jul 25, 2001 at 09:49 UTC | |
by snafu (Chaplain) on Jul 26, 2001 at 07:22 UTC | |
|
Re: hash of arrays -- Im missing something
by japhy (Canon) on Jul 25, 2001 at 10:53 UTC | |
by snafu (Chaplain) on Jul 26, 2001 at 07:26 UTC | |
|
Re: hash of arrays -- Im missing something
by jepri (Parson) on Jul 25, 2001 at 09:59 UTC | |
|
Re: hash of arrays -- Im missing something
by physi (Friar) on Jul 25, 2001 at 09:52 UTC | |
by snafu (Chaplain) on Jul 26, 2001 at 07:24 UTC | |
|
Re: hash of arrays -- Im missing something
by andye (Curate) on Jul 25, 2001 at 16:03 UTC | |
|
Re: hash of arrays -- Im missing something
by snafu (Chaplain) on Jul 26, 2001 at 07:29 UTC |