<div dir="ltr">Does the game always involve exactly two players? If so, it might be clearest to just define %OPPONENT without a loop or map.</div><div class="gmail_extra"><br><div class="gmail_quote">On Tue, Sep 26, 2017 at 11:17 AM, Shlomi Fish <span dir="ltr"><<a href="mailto:shlomif@shlomifish.org" target="_blank">shlomif@shlomifish.org</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Hi Tom,<br>
<div><div class="h5"><br>
On Tue, 26 Sep 2017 13:53:25 -0400<br>
Tom Legrady <<a href="mailto:legrady@gmail.com">legrady@gmail.com</a>> wrote:<br>
<br>
> As an exercise, I'm implementing a RosettaCode challenge for a game<br>
> between human and computer.<br>
><br>
> const my $PLAYERS => qw( human computer );<br>
><br>
> I want to automatically access the appropriate opponent for each player,<br>
> which allows core game logic of<br>
><br>
> while ( $no_one_won ) {<br>
> for my $player ( @PLAYERS ) {<br>
> $self->player( $player )->take_turn( $OPPONENT{$player} );<br>
> }<br>
> }<br>
><br>
> But to initialize %OPPONENT, I would need to nest a grep{} inside a<br>
> map{}, which leads to two instances of $_. Now perhaps the specification<br>
> of the opponent should be part of the game object or of a player object,<br>
> but I'm still curious how to resolve this problem in a single stage. Now<br>
> obviously you can't compare $_ with $_ ...<br>
><br>
> const my %OPPONENT => map { $_ => grep { $_ ne $_ } @PLAYERS } @PLAYERS<br>
><br>
> If it weren't a const object, we could do this in stages, though it's<br>
> verbose and ugly:<br>
><br>
> my %OPPONENT;<br>
> for my $player ( @PLAYERS ) {<br>
> $OPPONENT{$player} = grep { $_ ne $player } @PLAYERS<br>
> }<br>
><br>
> But in the case of 'const my %OPPONENT', it's an error to attempt to<br>
> vivify a key after the object has become restricted.<br>
><br>
> It worked fine if I replaced the inner grep{} with a subroutine call,<br>
> not_me(), which would perform the grep away from the map{}. But then it<br>
> occurred to me to perform a variable assignment within the map{}. Once<br>
> we assign $a, we can safely redefined $_.<br>
><br>
> const my %OPPONENT => map { my $a = $_; $a => grep { $_ ne $a}<br>
> @PLAYERS } @PLAYERS<br>
><br>
<br>
</div></div>this seems like a good solution but you should not use $a as the name of a<br>
lexical var. See:<br>
<br>
<a href="http://perl-begin.org/tutorials/bad-elements/#overuse_dollar_underscore" rel="noreferrer" target="_blank">http://perl-begin.org/<wbr>tutorials/bad-elements/#<wbr>overuse_dollar_underscore</a><br>
<br>
<a href="http://perl-begin.org/tutorials/bad-elements/#vars-a-and-b" rel="noreferrer" target="_blank">http://perl-begin.org/<wbr>tutorials/bad-elements/#vars-<wbr>a-and-b</a><br>
<span class="im HOEnZb"><br>
<br>
<br>
> Tom<br>
> ______________________________<wbr>_________________<br>
> toronto-pm mailing list<br>
> <a href="mailto:toronto-pm@pm.org">toronto-pm@pm.org</a><br>
> <a href="http://mail.pm.org/mailman/listinfo/toronto-pm" rel="noreferrer" target="_blank">http://mail.pm.org/mailman/<wbr>listinfo/toronto-pm</a><br>
<br>
<br>
<br>
</span><span class="HOEnZb"><font color="#888888">--<br>
------------------------------<wbr>------------------------------<wbr>-----<br>
Shlomi Fish <a href="http://www.shlomifish.org/" rel="noreferrer" target="_blank">http://www.shlomifish.org/</a><br>
My Aphorisms - <a href="http://www.shlomifish.org/humour.html" rel="noreferrer" target="_blank">http://www.shlomifish.org/<wbr>humour.html</a><br>
<br>
* rindolf demands equal rights for years, minutes, hours and days to also get<br>
YAAKOV’S GREAT HUGE LOVE.<br>
— <a href="http://www.shlomifish.org/humour/fortunes/sharp-perl.html" rel="noreferrer" target="_blank">http://www.shlomifish.org/<wbr>humour/fortunes/sharp-perl.<wbr>html</a><br>
<br>
Please reply to list if it's a mailing list post - <a href="http://shlom.in/reply" rel="noreferrer" target="_blank">http://shlom.in/reply</a> .<br>
</font></span><div class="HOEnZb"><div class="h5">______________________________<wbr>_________________<br>
toronto-pm mailing list<br>
<a href="mailto:toronto-pm@pm.org">toronto-pm@pm.org</a><br>
<a href="http://mail.pm.org/mailman/listinfo/toronto-pm" rel="noreferrer" target="_blank">http://mail.pm.org/mailman/<wbr>listinfo/toronto-pm</a><br>
</div></div></blockquote></div><br></div>