Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Re: Fence vs. Posts

by QM (Parson)
on Jun 08, 2018 at 09:57 UTC ( [id://1216179]=note: print w/replies, xml ) Need Help??


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

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1216179]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (5)
As of 2024-04-25 08:08 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found