http://www.perlmonks.org?node_id=1023306


in reply to qw() and lists of lists

One way is with pairwise.

use List::MoreUtils qw( pairwise ); my @nametape = pairwise{ [ $a, $b ] } qw( 24 101 25 102 23 103 );

Update, correction: pairwise accepts a block and two lists. Doh!

What I was really after was the natatime function (also in List::MoreUtils):

use List::MoreUtils qw(natatime); my @list = qw( 24 101 25 102 23 103 ); my $it = natatime 2, @list; my @pairs; while ( my @vals = $it->() ) { push @pairs, \@vals; }

But frankly, the premise bugs me. It seems silly to jump through coding hoops and a runtime loop just to avoid typing [ 1,2 ], into the source code, in favor of qw( ... ). It doesn't improve code clarity, and doesn't improve code efficiency.

Thanks kcott for noticing and pointing out to me my mistakes. My defense: The two year old was throwing a fit while I typed the original solution. I think a screaming 2-year-old is probably more of a cognitive impairment than a few stiff drinks... but since I don't drink I'm probably not qualified to make the comparison.


Dave