foreach my $set (0..int(@allrows-1/$size_of_array_chunks)) ### generate the chunks (Explanation: with "$size_of_array_chunks = 100" this loop will do "@allrows[0..99]", then "@allrows[100..144]" if the File has 155 Rows) { my $testval = (@allrows[$size_of_array_chunks*$set..min($size_of_array_chunks*($set+1)-1,$#allrows)]); print "$testval\n"; do_sth(@allrows[$size_of_array_chunks*$set..min($size_of_array_chunks*($set+1)-1,$#allrows)]); #if the file has 17 lines, the code still runs 15 times even though the file is already at the end }