This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
apply patch for smarter AASSIGN_COMMON detection; regen headers
[perl5.git] / t / op / array.t
CommitLineData
a687059c
LW
1#!./perl
2
f1192cee 3print "1..37\n";
a687059c
LW
4
5@ary = (1,2,3,4,5);
6if (join('',@ary) eq '12345') {print "ok 1\n";} else {print "not ok 1\n";}
7
8$tmp = $ary[$#ary]; --$#ary;
9if ($tmp == 5) {print "ok 2\n";} else {print "not ok 2\n";}
10if ($#ary == 3) {print "ok 3\n";} else {print "not ok 3\n";}
11if (join('',@ary) eq '1234') {print "ok 4\n";} else {print "not ok 4\n";}
12
13$[ = 1;
14@ary = (1,2,3,4,5);
15if (join('',@ary) eq '12345') {print "ok 5\n";} else {print "not ok 5\n";}
16
17$tmp = $ary[$#ary]; --$#ary;
18if ($tmp == 5) {print "ok 6\n";} else {print "not ok 6\n";}
19if ($#ary == 4) {print "ok 7\n";} else {print "not ok 7\n";}
20if (join('',@ary) eq '1234') {print "ok 8\n";} else {print "not ok 8\n";}
21
22if ($ary[5] eq '') {print "ok 9\n";} else {print "not ok 9\n";}
23
a0d0e21e 24$#ary += 1; # see if element 5 gone for good
a687059c 25if ($#ary == 5) {print "ok 10\n";} else {print "not ok 10\n";}
a0d0e21e 26if (defined $ary[5]) {print "not ok 11\n";} else {print "ok 11\n";}
a687059c
LW
27
28$[ = 0;
29@foo = ();
30$r = join(',', $#foo, @foo);
31if ($r eq "-1") {print "ok 12\n";} else {print "not ok 12 $r\n";}
32$foo[0] = '0';
33$r = join(',', $#foo, @foo);
34if ($r eq "0,0") {print "ok 13\n";} else {print "not ok 13 $r\n";}
35$foo[2] = '2';
36$r = join(',', $#foo, @foo);
37if ($r eq "2,0,,2") {print "ok 14\n";} else {print "not ok 14 $r\n";}
38@bar = ();
39$bar[0] = '0';
40$bar[1] = '1';
41$r = join(',', $#bar, @bar);
42if ($r eq "1,0,1") {print "ok 15\n";} else {print "not ok 15 $r\n";}
43@bar = ();
44$r = join(',', $#bar, @bar);
45if ($r eq "-1") {print "ok 16\n";} else {print "not ok 16 $r\n";}
46$bar[0] = '0';
47$r = join(',', $#bar, @bar);
48if ($r eq "0,0") {print "ok 17\n";} else {print "not ok 17 $r\n";}
49$bar[2] = '2';
50$r = join(',', $#bar, @bar);
51if ($r eq "2,0,,2") {print "ok 18\n";} else {print "not ok 18 $r\n";}
52reset 'b';
53@bar = ();
54$bar[0] = '0';
55$r = join(',', $#bar, @bar);
56if ($r eq "0,0") {print "ok 19\n";} else {print "not ok 19 $r\n";}
57$bar[2] = '2';
58$r = join(',', $#bar, @bar);
59if ($r eq "2,0,,2") {print "ok 20\n";} else {print "not ok 20 $r\n";}
60
61$foo = 'now is the time';
62if (($F1,$F2,$Etc) = ($foo =~ /^(\S+)\s+(\S+)\s*(.*)/)) {
63 if ($F1 eq 'now' && $F2 eq 'is' && $Etc eq 'the time') {
64 print "ok 21\n";
65 }
66 else {
67 print "not ok 21\n";
68 }
69}
70else {
71 print "not ok 21\n";
72}
73
74$foo = 'lskjdf';
75if ($cnt = (($F1,$F2,$Etc) = ($foo =~ /^(\S+)\s+(\S+)\s*(.*)/))) {
76 print "not ok 22 $cnt $F1:$F2:$Etc\n";
77}
78else {
79 print "ok 22\n";
80}
81
82%foo = ('blurfl','dyick','foo','bar','etc.','etc.');
83%bar = %foo;
84print $bar{'foo'} eq 'bar' ? "ok 23\n" : "not ok 23\n";
85%bar = ();
86print $bar{'foo'} eq '' ? "ok 24\n" : "not ok 24\n";
87(%bar,$a,$b) = (%foo,'how','now');
88print $bar{'foo'} eq 'bar' ? "ok 25\n" : "not ok 25\n";
89print $bar{'how'} eq 'now' ? "ok 26\n" : "not ok 26\n";
90@bar{keys %foo} = values %foo;
91print $bar{'foo'} eq 'bar' ? "ok 27\n" : "not ok 27\n";
92print $bar{'how'} eq 'now' ? "ok 28\n" : "not ok 28\n";
93
94@foo = grep(/e/,split(' ','now is the time for all good men to come to'));
95print join(' ',@foo) eq 'the time men come' ? "ok 29\n" : "not ok 29\n";
96
97@foo = grep(!/e/,split(' ','now is the time for all good men to come to'));
98print join(' ',@foo) eq 'now is for all good to to' ? "ok 30\n" : "not ok 30\n";
79a0689e
LW
99
100$foo = join('',('a','b','c','d','e','f')[0..5]);
101print $foo eq 'abcdef' ? "ok 31\n" : "not ok 31\n";
102
103$foo = join('',('a','b','c','d','e','f')[0..1]);
104print $foo eq 'ab' ? "ok 32\n" : "not ok 32\n";
105
106$foo = join('',('a','b','c','d','e','f')[6]);
107print $foo eq '' ? "ok 33\n" : "not ok 33\n";
108
109@foo = ('a','b','c','d','e','f')[0,2,4];
110@bar = ('a','b','c','d','e','f')[1,3,5];
111$foo = join('',(@foo,@bar)[0..5]);
112print $foo eq 'acebdf' ? "ok 34\n" : "not ok 34\n";
113
114$foo = ('a','b','c','d','e','f')[0,2,4];
115print $foo eq 'e' ? "ok 35\n" : "not ok 35\n";
116
117$foo = ('a','b','c','d','e','f')[1];
118print $foo eq 'b' ? "ok 36\n" : "not ok 36\n";
a0231f0e 119
c6aa4a32
SP
120@foo = ( 'foo', 'bar', 'burbl');
121push(foo, 'blah');
f1192cee 122print $#foo == 3 ? "ok 37\n" : "not ok 37\n";