|
| Author |
Message |
m6246 Newbie

Joined: 08 Jul 2005 Posts: 15
 
|
Posted: Wed Jul 13, 2005 1:32 pm Post subject: store scores |
|
|
i have an array @emails that holds the emailaddresses of all participants in the convo.
@participants = $self->getMembers ($sock);
$tempstring = join(',', keys %{@participants});
@emails = split(',', $tempstring);
Now I want to store scores which are linked to the emailadresses during the conversation.
I made an array @scores to hold the scores
$numofparts = @participants;
for($x=0;$x<$numofparts+1;$x++){
@scores[$x]=0;
}
so all the elements are set to 0 for a start. Now I have linked the emails and scores by
@emails[x] has @scores[x]
Now for the hard part where im stuck..
I need to assign a new score to a certain email. So I need to search the @emails array for the emailaddress and then get the position of it so I can update the corresponding @scores element.
How do i go about searching and getting the correct position? Also, maybe theres a much easier way to store and link the emails/scores (I dont know much about perl)?
thanks |
|
| Back to top |
|
 |
Mojave Almost An Agent

Joined: 01 Nov 2003 Posts: 1434
 
|
Posted: Wed Jul 13, 2005 7:03 pm Post subject: |
|
|
You should read about hashes. Hashes give you lookup access to elements, which is what you want.
Now, on to some of the code you posted:
Not sure which version of MSN you are using, but in the latest, getMembers does not take a $sock argument. Also, it returns a hashref, not an array. In addition, the keys to that hash are emails, so you don't need that join and split code.
| Code: | my $members = $self->getMemebers();
my @emails = keys %$members; |
To get the size of an array, use the scalar keyword. And when you access or set elements of an array, you need to understand that they are scalars, not arrays, so you use $ instead of @.
Finally, use keyword my whenever you create variables - you'll save yourself a lot of headaches in the future when you start writing complicated code.
But anyway, back to your problem. Simply use a hash (or hashref which is easier to manipulate) to store your scores.
| Code: | my $scores = {};
$scores->{'some_email'} = 0;
$scores->{'another_email'} = 10;
# etc. |
|
|
| Back to top |
|
 |
m6246 Newbie

Joined: 08 Jul 2005 Posts: 15
 
|
Posted: Wed Jul 13, 2005 7:21 pm Post subject: |
|
|
| can i store more variables per emailaddress? |
|
| Back to top |
|
 |
Cer Upgraded Agent

Joined: 03 Feb 2004 Posts: 3776 Location: Michigan
  votes: 4
|
Posted: Wed Jul 13, 2005 7:36 pm Post subject: |
|
|
| m6246 wrote: | | can i store more variables per emailaddress? |
Yeah, just go another hashref deep.
$scores->{'some_email'}->{points} = 10;
$scores->{'some_email'}->{game} = 'tictactoe';
$scores->{'some_email'}->{opponent} = 'another_email';
those would all store variables for the same user "some_email" _________________ Current Site (2008) http://www.cuvou.com/ |
|
| Back to top |
|
 |
m6246 Newbie

Joined: 08 Jul 2005 Posts: 15
 
|
Posted: Wed Jul 13, 2005 8:15 pm Post subject: |
|
|
| gonna look into that, thanks a lot! |
|
| Back to top |
|
 |
m6246 Newbie

Joined: 08 Jul 2005 Posts: 15
 
|
Posted: Wed Jul 13, 2005 8:24 pm Post subject: |
|
|
one last question though.. getMembers() returns the emailaddress
is there also a method that returns the friendly nick of the participant? |
|
| Back to top |
|
 |
Mojave Almost An Agent

Joined: 01 Nov 2003 Posts: 1434
 
|
Posted: Wed Jul 13, 2005 8:31 pm Post subject: |
|
|
| getMembers returns a hash (really a hash reference), the keys are the emails, the values are the friendly names. |
|
| Back to top |
|
 |
m6246 Newbie

Joined: 08 Jul 2005 Posts: 15
 
|
Posted: Wed Jul 13, 2005 9:03 pm Post subject: |
|
|
| $scores{$id} returns HASHx38028 |
|
| Back to top |
|
 |
Cer Upgraded Agent

Joined: 03 Feb 2004 Posts: 3776 Location: Michigan
  votes: 4
|
Posted: Wed Jul 13, 2005 9:18 pm Post subject: |
|
|
| m6246 wrote: | | $scores{$id} returns HASHx38028 |
There's a difference between hashes and hashrefs.
$scores{$id} is a hash.
$scores->{$id} is a hashref.
So maybe you wanted $scores->{$id} _________________ Current Site (2008) http://www.cuvou.com/ |
|
| Back to top |
|
 |
m6246 Newbie

Joined: 08 Jul 2005 Posts: 15
 
|
Posted: Thu Jul 14, 2005 4:01 pm Post subject: |
|
|
ok im understanding the hashes but what im not getting to work (see below) is storing an array in a hash. How is this done?
$game{$id}->{$something}->{@emails} |
|
| Back to top |
|
 |
Mojave Almost An Agent

Joined: 01 Nov 2003 Posts: 1434
 
|
Posted: Thu Jul 14, 2005 4:25 pm Post subject: |
|
|
You can only store scalars in a hash. So you need to get a reference to the array and store that. References are scalars.
| Code: | my $arrayref = \@array;
$hash->{'emails'} = $arrayref; |
|
|
| Back to top |
|
 |
Cer Upgraded Agent

Joined: 03 Feb 2004 Posts: 3776 Location: Michigan
  votes: 4
|
Posted: Thu Jul 14, 2005 6:10 pm Post subject: |
|
|
You can also define arrayrefs too.
| Code: | $hash->{'emails'} = [
@some_array,
@another_array,
'a',
'new',
'set',
'of',
'values',
]; |
That code would combine those two arrays with even more array items to make one big array.
Just for example though.
And then to get like the 4th item from your arrayref:
| Code: | | $hash->{'emails'}->[3]; |
(note that arrays start at 0)
So it's similar to arrays, except in declaring an arrayref you use square brackets, and then to get a value you need the arrow thing -> like in a hashref... and the square brackets as always. _________________ Current Site (2008) http://www.cuvou.com/ |
|
| Back to top |
|
 |
|