10 use vars qw(@array @r $k $v $c);
14 @array = qw(crunch zam bloop);
17 is (scalar @r, 2, "'each' on array returns index and value of next element");
18 is ($r[0], 0, "got expected index");
19 is ($r[1], 'crunch', "got expected value");
20 ($k, $v) = each @array;
21 is ($k, 1, "got expected index of next element");
22 is ($v, 'zam', "got expected value of next element");
23 ($k, $v) = each @array;
24 is ($k, 2, "got expected index of remaining element");
25 is ($v, 'bloop', "got expected value of remaining element");
28 "no elements remaining to be iterated over in original array");
31 is (scalar @r, 2, "start second iteration over original array");
32 is ($r[0], 0, "got expected index");
33 is ($r[1], 'crunch', "got expected value");
35 is ($k, 1, "got index when only index was assigned to variable");
37 my @lex_array = qw(PLOP SKLIZZORCH RATTLE);
39 (@r) = each @lex_array;
40 is (scalar @r, 2, "'each' on array returns index and value of next element");
41 is ($r[0], 0, "got expected index");
42 is ($r[1], 'PLOP', "got expected value");
43 ($k, $v) = each @lex_array;
44 is ($k, 1, "got expected index of next element");
45 is ($v, 'SKLIZZORCH', "got expected value of next element");
46 ($k) = each @lex_array;
47 is ($k, 2, "got expected index of remaining element");
48 (@r) = each @lex_array;
50 "no elements remaining to be iterated over in original array");
56 "'each' on array inside reference returns index and value of next element");
57 is ($r[0], 0, "got expected index");
58 is ($r[1], 'bacon', "got expected value of array element inside reference");
62 "no elements remaining to be iterated over in array inside reference");
64 is (each @$ar, 0, "scalar context 'each' on array returns expected index");
65 is (scalar each @$ar, undef,
66 "no elements remaining to be iterated over; array reference case");
71 "'keys' on array in list context returns list of indices");
73 @keys = keys @lex_array;
75 "'keys' on another array in list context returns list of indices");
77 ($k, $v) = each @array;
78 is ($k, 0, "got expected index");
79 is ($v, 'crunch', "got expected value");
83 "'keys' on array in list context returns list of indices");
85 ($k, $v) = each @array;
86 is ($k, 0, "following 'keys', got expected index");
87 is ($v, 'crunch', "following 'keys', got expected value");
92 @values = values @array;
93 is ("@values", "@array",
94 "'values' on array returns list of values");
96 @values = values @lex_array;
97 is ("@values", "@lex_array",
98 "'values' on another array returns list of values");
100 ($k, $v) = each @array;
101 is ($k, 0, "following 'values', got expected index");
102 is ($v, 'crunch', "following 'values', got expected index");
104 @values = values @array;
105 is ("@values", "@array",
106 "following 'values' and 'each', 'values' continues to return expected list of values");
108 ($k, $v) = each @array;
110 "following 'values', 'each' and 'values', 'each' continues to return expected index");
112 "following 'values', 'each' and 'values', 'each' continues to return expected value");
115 while (each @array) { }
117 # each(ARRAY) in the conditional loop
119 while (($k, $v) = each @array) {
120 is ($k, $c, "'each' on array in loop returns expected index '$c'");
122 "'each' on array in loop returns expected value '$array[$k]'");
126 # each(ARRAY) on scalar context in conditional loop
127 # should guarantee to be wrapped into defined() function.
128 # first return value will be 0 --> [#90888]
132 while ($k = each @array) {
134 "'each' on array in scalar context in loop returns expected index '$v'");
138 # each(ARRAY) in the conditional loop
140 for (; ($k, $v) = each @array ;) {
142 "'each' on array in list context in loop returns expected index '$c'");
144 "'each' on array in list context in loop returns expected value '$array[$k]'");
148 # each(ARRAY) on scalar context in conditional loop
153 for (; $k = each(@array) ;) {
155 "'each' on array in scalar context in loop returns expected index '$v'");
159 # Reset the iterator when the array is cleared [RT #75596]
162 my ($i, $v) = each @a;
163 is ("$i-$v", '0-a', "got expected index and value");
167 "got expected new index and value after array gets new content");
170 # Check that the iterator is reset when localization ends
173 my ($i, $v) = each @array;
174 is ("$i-$v", '0-a', "got expected index and value");
176 local @array = 'A' .. 'C';
177 my ($i, $v) = each @array;
179 "got expected new index and value after array is localized and gets new content");
180 ($i, $v) = each @array;
182 "got expected next index and value after array is localized and gets new content");
184 ($i, $v) = each @array;
186 "got expected next index and value upon return to pre-localized array");
188 while (each @array) { }