in reply to removing elements from an array
Here's another option:
use Modern::Perl; use Data::Dumper; my @array = qw/1 A 2 B 3 C 4 D 5 E/; my $i = 0; say 'Before:'; say Dumper \@array; @array = grep !($i++ % 2), @array; say 'After:'; say Dumper \@array;
Outout:
Before: $VAR1 = [ '1', 'A', '2', 'B', '3', 'C', '4', 'D', '5', 'E' ]; After: $VAR1 = [ '1', '2', '3', '4', '5' ];
This uses the modulo operator within grep to let only the even-numbered elements of the array pass.
Hope this helps!
|
|---|