11 # delete() on hash elements
19 $foo = delete $foo{2};
21 cmp_ok($foo,'eq','b','delete 2');
22 ok(!(exists $foo{2}),'b absent');
23 cmp_ok($foo{1},'eq','a','a exists');
24 cmp_ok($foo{3},'eq','c','c exists');
25 cmp_ok($foo{4},'eq','d','d exists');
26 cmp_ok($foo{5},'eq','e','e exists');
28 @foo = delete @foo{4, 5};
30 cmp_ok(scalar(@foo),'==',2,'deleted slice');
31 cmp_ok($foo[0],'eq','d','slice 1');
32 cmp_ok($foo[1],'eq','e','slice 2');
33 ok(!(exists $foo{4}),'d absent');
34 ok(!(exists $foo{5}),'e absent');
35 cmp_ok($foo{1},'eq','a','a still exists');
36 cmp_ok($foo{3},'eq','c','c still exists');
38 $foo = join('',values(%foo));
39 ok($foo eq 'ac' || $foo eq 'ca','remaining keys');
41 foreach $key (keys %foo) {
48 $foo = join('',values(%foo));
49 ok($foo eq 'xy' || $foo eq 'yx','fresh keys');
51 $refhash{"top"}->{"foo"} = "FOO";
52 $refhash{"top"}->{"bar"} = "BAR";
54 delete $refhash{"top"}->{"bar"};
55 @list = keys %{$refhash{"top"}};
57 cmp_ok("@list",'eq',"foo", 'autoviv and delete hashref');
61 my($a) = \(values %a);
63 my $c = \delete $a{bar};
65 ok($a == $b && $b == $c,'a b c equivalent');
68 # delete() on array elements
77 $foo = delete $foo[2];
79 cmp_ok($foo,'eq','b','ary delete 2');
80 ok(!(exists $foo[2]),'ary b absent');
81 cmp_ok($foo[1],'eq','a','ary a exists');
82 cmp_ok($foo[3],'eq','c','ary c exists');
83 cmp_ok($foo[4],'eq','d','ary d exists');
84 cmp_ok($foo[5],'eq','e','ary e exists');
86 @bar = delete @foo[4,5];
88 cmp_ok(scalar(@bar),'==',2,'ary deleted slice');
89 cmp_ok($bar[0],'eq','d','ary slice 1');
90 cmp_ok($bar[1],'eq','e','ary slice 2');
91 ok(!(exists $foo[4]),'ary d absent');
92 ok(!(exists $foo[5]),'ary e absent');
93 cmp_ok($foo[1],'eq','a','ary a still exists');
94 cmp_ok($foo[3],'eq','c','ary c still exists');
97 cmp_ok($foo,'eq','ac','ary elems');
98 cmp_ok(scalar(@foo),'==',4,'four is the number thou shalt count');
100 foreach $key (0 .. $#foo) {
104 cmp_ok(scalar(@foo),'==',0,'and then there were none');
110 cmp_ok($foo,'eq','x y','two fresh');
112 $refary[0]->[0] = "FOO";
113 $refary[0]->[3] = "BAR";
115 delete $refary[0]->[3];
117 cmp_ok( scalar(@{$refary[0]}),'==',1,'one down');
123 my $c = \delete $a[bar];
125 ok($a == $b && $b == $c,'a b c also equivalent');
130 my ($x,$y) = (1, scalar delete @h{()});
131 ok(!defined($y),q([perl #29127] scalar delete of empty slice returned garbage));
136 sub X::DESTROY { $x++ }
139 $a[0] = bless [], 'X';
140 my $y = delete $a[0];
142 cmp_ok($x,'==',1,q([perl #30733] array delete didn't free returned element));