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
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.