Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Re: Array confusion.

by Mr. Muskrat (Canon)
on Feb 14, 2017 at 22:07 UTC ( [id://1182013]=note: print w/replies, xml ) Need Help??


in reply to Array confusion.

You are modifying @arr inside the loop in such a way that $thing containing 'a' at each iteration. It's easier to see with some modifications:

use warnings; use strict; use diagnostics; my @arr = ('a'..'z'); print "@arr\n"; foreach my $thing (@arr) { print "[$thing]"; my $ele = pop @arr; my $stuff = unshift @arr, $ele; print "$ele - @arr\n"; } __END__ a b c d e f g h i j k l m n o p q r s t u v w x y z [a]z - z a b c d e f g h i j k l m n o p q r s t u v w x y [a]y - y z a b c d e f g h i j k l m n o p q r s t u v w x [a]x - x y z a b c d e f g h i j k l m n o p q r s t u v w [a]w - w x y z a b c d e f g h i j k l m n o p q r s t u v [a]v - v w x y z a b c d e f g h i j k l m n o p q r s t u [a]u - u v w x y z a b c d e f g h i j k l m n o p q r s t [a]t - t u v w x y z a b c d e f g h i j k l m n o p q r s [a]s - s t u v w x y z a b c d e f g h i j k l m n o p q r [a]r - r s t u v w x y z a b c d e f g h i j k l m n o p q [a]q - q r s t u v w x y z a b c d e f g h i j k l m n o p [a]p - p q r s t u v w x y z a b c d e f g h i j k l m n o [a]o - o p q r s t u v w x y z a b c d e f g h i j k l m n [a]n - n o p q r s t u v w x y z a b c d e f g h i j k l m [a]m - m n o p q r s t u v w x y z a b c d e f g h i j k l [a]l - l m n o p q r s t u v w x y z a b c d e f g h i j k [a]k - k l m n o p q r s t u v w x y z a b c d e f g h i j [a]j - j k l m n o p q r s t u v w x y z a b c d e f g h i [a]i - i j k l m n o p q r s t u v w x y z a b c d e f g h [a]h - h i j k l m n o p q r s t u v w x y z a b c d e f g [a]g - g h i j k l m n o p q r s t u v w x y z a b c d e f [a]f - f g h i j k l m n o p q r s t u v w x y z a b c d e [a]e - e f g h i j k l m n o p q r s t u v w x y z a b c d [a]d - d e f g h i j k l m n o p q r s t u v w x y z a b c [a]c - c d e f g h i j k l m n o p q r s t u v w x y z a b [a]b - b c d e f g h i j k l m n o p q r s t u v w x y z a [a]a - a b c d e f g h i j k l m n o p q r s t u v w x y z

Before the loop @arr contains 'a'..'z' as expected.

First iteration: $thing ($arr[0]) contains 'a', $ele ($arr[25]) contains 'z' which goes to the beginning and now 'a' is at $arr[1].

Second iteration: $thing ($arr[1]) contains 'a', $ele ($arr[25]) contains 'y' which goes to the beginning and now 'a' is at $arr[2].

Wash, rinse, repeat until you've reached $arr[25].

update: removed meaningless comment

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1182013]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (6)
As of 2024-04-19 09:59 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found