The key line is:
splice @cities, (@cities-@states)+1, 0, shift @states for 0 .. @states +-1;
Note the "for" at the end of that line; it treats this as:
for ( 0..@states-1 ) { splice @cities, ( @cities-@states)+1, 0, shift @states; }
Or:
for ( 0..@states-1 ) { my $state = shift @states; splice @cities, ( @cities-@states)+1, 0, $state; }
The splice cuts no elements (that's the 0 part) from the @cities array, but puts in the first element (after removing it) from the @states array in the indicated position. The math on that position is such that it will be ever odd position until the states around is exhausted (that is, it first is 1, then after adding a new element to @cities and removing it from @states, it will be 3, etc.).

In other words, this intermingles the two arrays, destroying one and adding those to the other.

A better way to write this, IMO, in order to improve readibility is:

@citystates = map { $cities[$_], $states[$_] } (0..@cities-1);
This way, you don't have to destroy either array. The only catch is that it assumes the arrays to be equal sizes to start.

-----------------------------------------------------
Dr. Michael K. Neylon - mneylon-pm@masemware.com || "You've left the lens cap of your mind on again, Pinky" - The Brain
"I can see my house from here!"
It's not what you know, but knowing how to find it if you don't know that's important


In reply to Re: splice and for loop together?? by Masem
in thread splice and for loop together?? by Wyman G

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.