[tpm] Dealing with nested map { grep {} }

Viktor Pavlenko vvp at cogeco.ca
Tue Sep 26 14:28:43 PDT 2017


>>>>> "TL" == Tom Legrady <legrady at gmail.com> writes:

    TL> It worked fine if I replaced the inner grep{} with a subroutine call, 
    TL> not_me(), which would perform the grep away from the map{}. But then it 
    TL> occurred to me to perform a variable assignment within the map{}. Once 
    TL> we assign $a, we can safely redefined $_.

    TL>      const my %OPPONENT => map { my $a = $_; $a => grep { $_ ne $a} 
    TL> @PLAYERS } @PLAYERS

The opponents array of a player is the players array excluding the player in
question. What about going through the list once and use slices?

perl -MData::Dumper -le '@ar = qw/a b c d e f g/; $hash{$ar[$_]} = [ @ar[0 .. $_ - 1, $_ + 1 .. $#ar] ] for 0 .. $#ar; print Dumper \%hash'

-- 
Viktor


More information about the toronto-pm mailing list