in reply to complementary array indices
How big are your arrays? An CPU inefficient but memory efficient version is:
my @a_indices = (1,3,5); my @b = map {my $t = $_; (grep {$t == $_} @a_indices) ? undef : $t} 0 +.. 9; print join ', ', grep {defined} @b;
Prints:
0, 2, 4, 6, 7, 8, 9
On the other hand:
my @a_indices = (1,3,5); my %hasIndex = map {$_ => undef} @a_indices; my @b = map {exists $hasIndex{$_} ? undef : $_} 0 .. 9;
is likely to be faster for large arrays, but at the expense of creating a hash.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: complementary array indices
by tilly (Archbishop) on May 11, 2009 at 18:29 UTC |