ivanthemad has asked for the wisdom of the Perl Monks concerning the following question:
Most mystical of monks, I seek your wisdom today regarding what is undoubtedly an imperfect understanding of Perl arrays.
I have an array of integers, and I would like to pair every two integers. Perhaps I should simply use a for-loop, but I am curious as to why my first solution does not work.
My understanding is that the while will execute so long as there are elements in @a, and that shift will return the left-most element of the array, move all elements down by one, and shorten the array by one. I expected to receive as output:use strict; use warnings; my $s = "foo bar baz qux 3 3 1 3"; my @a = split /\s/, $s; splice(@a, 0, 4); while (@a) { print (shift @a), (shift @a), "\n"; }
But instead received:33 13
Please direct this flummoxed novice to an explanation which said novice failed to find via either RTFM or STFW. I would be most grateful!31 # and no newline!
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: arrays: shifting in while loop
by BrowserUk (Patriarch) on Mar 06, 2012 at 14:39 UTC | |
by ivanthemad (Initiate) on Mar 06, 2012 at 14:47 UTC | |
by kcott (Archbishop) on Mar 06, 2012 at 15:09 UTC | |
by Anonymous Monk on Mar 06, 2012 at 14:50 UTC | |
by JavaFan (Canon) on Mar 06, 2012 at 15:07 UTC | |
Re: arrays: shifting in while loop
by kcott (Archbishop) on Mar 06, 2012 at 14:52 UTC | |
Re: arrays: shifting in while loop
by AnomalousMonk (Archbishop) on Mar 06, 2012 at 21:36 UTC | |
by repellent (Priest) on Mar 07, 2012 at 05:01 UTC | |
by AnomalousMonk (Archbishop) on Mar 07, 2012 at 15:17 UTC | |
by repellent (Priest) on Mar 07, 2012 at 17:07 UTC |
Back to
Seekers of Perl Wisdom