How can I do a for() or foreach() loop in Python and Perl, respectively, that only prints every third index? I need to move every third index to a new array.
Perl:
As with draegtun's answer, but using a count var:
my $i;
my @new = grep {not ++$i % 3} @list;
Python
print list[::3] # print it
newlist = list[::3] # copy it
Perl
for ($i = 0; $i < @list; $i += 3) {
print $list[$i]; # print it
push @y, $list[$i]; # copy it
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With