⭐ in reply to How do I loop through a list two or more elements at a time?
For a real answer now: create your list as an array of arrays, loop on the outer array.
print "$_->[0] - $_->[1]\n" for @LoL;
If you start with a flat list, you create the LoL with davorg's solution:
or non-destructive:my @LoL; while(my @items = splice( @list, 0, 2) ){ push @LoL, [@items]; }
my $size = 2; die "Uneven list" if @list % $size; my @LoL; for my $index ( 0.. $#list ){ my $rem = $index % $size; $LoL[($index - $rem)/$size]->[$rem] = $list[$index]; }
In Section
Seekers of Perl Wisdom