http://qs321.pair.com?node_id=1216179


in reply to Fence vs. Posts

I often have a list where I need to do adjacent pairs.
my @trip = ("Chicago", "Saint Looey", "Joplin", "OKC", "Amarillo", " +Gallup", "Flagstaff", "Winona", "Kingman", "Barstow", +"San Bernandino", "LA" ); map { printf "%15s to %-15s\n", @trip[$_..$_+1] } 0..$#trip-1;

which emits:

Chicago to Saint Looey Saint Looey to Joplin Joplin to OKC OKC to Amarillo Amarillo to Gallup Gallup to Flagstaff Flagstaff to Winona Winona to Kingman Kingman to Barstow Barstow to San Bernandino San Bernandino to LA

If you did this often, you might want a pairs_overlapping function. Or, going with a generic version:

subsequence(length=>3, step=>1, list=>\@trip);

a function that returns an arbitrary list of adjacent elements, with an arbitrary step size.

As to the next bit, I think several good solutions have been shown by others.

-QM
--
Quantum Mechanics: The dreams stuff is made of