in reply to Reducing array
And then I thought about it. Hang on. The array's getting spliced each time a match is made, so an index of -$_ would skip elements (ie $array[-4] before is 'cow', after, it's 'dog', which is expected, but... Weird thing is, it doesn't look like it should work. Anyone got any idea why this works when I think it shouldn't???# First, but @items into a hash instead though... my @array = qw(dog cat cow horse donkey chicken); my %items = qw(cat 1 donkey 1); # this is weird.. if this is true print "\$array[-4] = $array[-4]\n"; for (1..@array) { splice(@array, -$_, 1) if $items{ $array[-$_] }; } # and this is true print "\$array[-4] = $array[-4]\n"; # how come this is right??? print join ', ',@array, "\n";
err
cLive ;-)
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: (cLive ;-) Re: Reducing array
by jmcnamara (Monsignor) on May 09, 2002 at 10:42 UTC |