Wyman G has asked for the wisdom of the Perl Monks concerning the following question:

Can someone tell me where I can go to get info about the following for loop construct. I stumbled onto this and it worksbut I don't know why it works. It doesn't have the forsyntax. It is being used with shift. Confused...
@cities = (Atlanta, Birmingham, Chicago, Memphis, "New York"); @states = (GA, AL, IL, TN, NY); print "Cities: @cities" . "\n"; print "States: @states" . "\n"; splice @cities, (@cities-@states)+1, 0, shift @states for 0 .. @states +-1; print "@cities" . "\n";

Edit Masem 2001-12-21 - CODE tags

Replies are listed 'Best First'.
Re: splice and for loop together??
by Masem (Monsignor) on Dec 22, 2001 at 00:46 UTC
    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

(crazyinsomniac) Re: splice and for loop together??
by crazyinsomniac (Prior) on Dec 22, 2001 at 15:26 UTC
    When you see some syntaxt that you can't quite make sense of, the O module can really help, for example:
    dos>perl -MO=Deparse -e"splice @cities, (@cities-@states)+1, 0, shift +@states for 0 .. @states-1;" for 0 .. @states-1;" foreach $_ (0 .. @states - 1) { splice @cities, @cities - @states + 1, 0, shift @states; } -e syntax OK dos> dos> dos>more eff @cities = (Atlanta, Birmingham, Chicago, Memphis, "New York"); @states = (GA, AL, IL, TN, NY); print "Cities: @cities" . "\n"; print "States: @states" . "\n"; splice @cities, (@cities-@states)+1, 0, shift @states for 0 .. @states +-1; print "@cities" . "\n"; dos> dos> dos>perl -MO=Deparse eff @cities = ('Atlanta', 'Birmingham', 'Chicago', 'Memphis', 'New York'); @states = ('GA', 'AL', 'IL', 'TN', 'NY'); print "Cities: @cities" . "\n"; print "States: @states" . "\n"; foreach $_ (0 .. @states - 1) { splice @cities, @cities - @states + 1, 0, shift @states; } print "@cities" . "\n"; eff syntax OK dos>
    perlsyn is always good reading material.

     
    ___crazyinsomniac_______________________________________
    Disclaimer: Don't blame. It came from inside the void

    perl -e "$q=$_;map({chr unpack qq;H*;,$_}split(q;;,q*H*));print;$q/$q;"