How can I print 1st, 10th, 20th… lines (not array index) number in a long list of text.
Of course, the following doesn’t work:
for(my $i=0; $i<=$arr_size; $i+=10){
print $arr[$i],"\n";
}
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
If you are reading from a filehandle:
If you have a scalar that holds a bunch of lines like:
Then you can treat the scalar like a file by passing a reference to it during an
open:and then use the solution above.
Putting it all together, you get
Note, this trick only works if you have built
perlwith PerlIO on, but that has been the default since Perl 5.8. You will need to grabIO::Scalarfrom CPAN if your version ofperlwasn’t compiled with Perl IO.For truly insane levels of weirdness, you could use
Tie::Fileon the in-memory file: