in reply to splice and for loop together??
Note the "for" at the end of that line; it treats this as:splice @cities, (@cities-@states)+1, 0, shift @states for 0 .. @states +-1;
Or:for ( 0..@states-1 ) { splice @cities, ( @cities-@states)+1, 0, shift @states; }
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.).for ( 0..@states-1 ) { my $state = shift @states; splice @cities, ( @cities-@states)+1, 0, $state; }
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:
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.@citystates = map { $cities[$_], $states[$_] } (0..@cities-1);
-----------------------------------------------------
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
|
|---|