Commit | Line | Data |
---|---|---|
8d063cd8 LW |
1 | #!./perl |
2 | ||
79072805 | 3 | # $RCSfile: split.t,v $$Revision: 4.1 $$Date: 92/08/07 18:28:26 $ |
8d063cd8 | 4 | |
815d35b9 | 5 | print "1..26\n"; |
8d063cd8 LW |
6 | |
7 | $FS = ':'; | |
8 | ||
9 | $_ = 'a:b:c'; | |
10 | ||
11 | ($a,$b,$c) = split($FS,$_); | |
12 | ||
13 | if (join(';',$a,$b,$c) eq 'a;b;c') {print "ok 1\n";} else {print "not ok 1\n";} | |
14 | ||
15 | @ary = split(/:b:/); | |
16 | if (join("$_",@ary) eq 'aa:b:cc') {print "ok 2\n";} else {print "not ok 2\n";} | |
17 | ||
18 | $_ = "abc\n"; | |
a687059c | 19 | @xyz = (@ary = split(//)); |
8d063cd8 LW |
20 | if (join(".",@ary) eq "a.b.c.\n") {print "ok 3\n";} else {print "not ok 3\n";} |
21 | ||
22 | $_ = "a:b:c::::"; | |
23 | @ary = split(/:/); | |
24 | if (join(".",@ary) eq "a.b.c") {print "ok 4\n";} else {print "not ok 4\n";} | |
2e1b3b7e | 25 | |
378cc40b LW |
26 | $_ = join(':',split(' '," a b\tc \t d ")); |
27 | if ($_ eq 'a:b:c:d') {print "ok 5\n";} else {print "not ok 5 #$_#\n";} | |
2e1b3b7e KK |
28 | |
29 | $_ = join(':',split(/ */,"foo bar bie\tdoll")); | |
30 | if ($_ eq "f:o:o:b:a:r:b:i:e:\t:d:o:l:l") | |
31 | {print "ok 6\n";} else {print "not ok 6\n";} | |
378cc40b LW |
32 | |
33 | $_ = join(':', 'foo', split(/ /,'a b c'), 'bar'); | |
34 | if ($_ eq "foo:a:b::c:bar") {print "ok 7\n";} else {print "not ok 7 $_\n";} | |
35 | ||
a687059c LW |
36 | # Can we say how many fields to split to? |
37 | $_ = join(':', split(' ','1 2 3 4 5 6', 3)); | |
38 | print $_ eq '1:2:3 4 5 6' ? "ok 8\n" : "not ok 8 $_\n"; | |
39 | ||
40 | # Can we do it as a variable? | |
41 | $x = 4; | |
42 | $_ = join(':', split(' ','1 2 3 4 5 6', $x)); | |
43 | print $_ eq '1:2:3:4 5 6' ? "ok 9\n" : "not ok 9 $_\n"; | |
44 | ||
45 | # Does the 999 suppress null field chopping? | |
46 | $_ = join(':', split(/:/,'1:2:3:4:5:6:::', 999)); | |
47 | print $_ eq '1:2:3:4:5:6:::' ? "ok 10\n" : "not ok 10 $_\n"; | |
48 | ||
49 | # Does assignment to a list imply split to one more field than that? | |
68dc0745 | 50 | if ($^O eq 'MSWin32') { $foo = `.\\perl -D1024 -e "(\$a,\$b) = split;" 2>&1` } |
f0963acb | 51 | elsif ($^O eq 'VMS') { $foo = `./perl "-D1024" -e "(\$a,\$b) = split;" 2>&1` } |
68dc0745 | 52 | else { $foo = `./perl -D1024 -e '(\$a,\$b) = split;' 2>&1` } |
bfe546ed | 53 | print $foo =~ /DEBUGGING/ || $foo =~ /SV = (VOID|IV\(3\))/ ? "ok 11\n" : "not ok 11\n"; |
a687059c LW |
54 | |
55 | # Can we say how many fields to split to when assigning to a list? | |
56 | ($a,$b) = split(' ','1 2 3 4 5 6', 2); | |
57 | $_ = join(':',$a,$b); | |
58 | print $_ eq '1:2 3 4 5 6' ? "ok 12\n" : "not ok 12 $_\n"; | |
59 | ||
084811a7 | 60 | # do subpatterns generate additional fields (without trailing nulls)? |
61 | $_ = join '|', split(/,|(-)/, "1-10,20,,,"); | |
62 | print $_ eq "1|-|10||20" ? "ok 13\n" : "not ok 13\n"; | |
63 | ||
64 | # do subpatterns generate additional fields (with a limit)? | |
65 | $_ = join '|', split(/,|(-)/, "1-10,20,,,", 10); | |
66 | print $_ eq "1|-|10||20||||||" ? "ok 14\n" : "not ok 14\n"; | |
e1fa4fd3 HS |
67 | |
68 | # is the 'two undefs' bug fixed? | |
69 | (undef, $a, undef, $b) = qw(1 2 3 4); | |
70 | print "$a|$b" eq "2|4" ? "ok 15\n" : "not ok 15\n"; | |
71 | ||
72 | # .. even for locals? | |
73 | { | |
74 | local(undef, $a, undef, $b) = qw(1 2 3 4); | |
75 | print "$a|$b" eq "2|4" ? "ok 16\n" : "not ok 16\n"; | |
76 | } | |
fb73857a | 77 | |
78 | # check splitting of null string | |
79 | $_ = join('|', split(/x/, '',-1), 'Z'); | |
80 | print $_ eq "Z" ? "ok 17\n" : "#$_\nnot ok 17\n"; | |
81 | ||
82 | $_ = join('|', split(/x/, '', 1), 'Z'); | |
83 | print $_ eq "Z" ? "ok 18\n" : "#$_\nnot ok 18\n"; | |
84 | ||
85 | $_ = join('|', split(/(p+)/,'',-1), 'Z'); | |
86 | print $_ eq "Z" ? "ok 19\n" : "#$_\nnot ok 19\n"; | |
87 | ||
88 | $_ = join('|', split(/.?/, '',-1), 'Z'); | |
89 | print $_ eq "Z" ? "ok 20\n" : "#$_\nnot ok 20\n"; | |
90 | ||
c277df42 IZ |
91 | |
92 | # Are /^/m patterns scanned? | |
93 | $_ = join '|', split(/^a/m, "a b a\na d a", 20); | |
94 | print $_ eq "| b a\n| d a" ? "ok 21\n" : "not ok 21\n# `$_'\n"; | |
95 | ||
96 | # Are /$/m patterns scanned? | |
97 | $_ = join '|', split(/a$/m, "a b a\na d a", 20); | |
98 | print $_ eq "a b |\na d |" ? "ok 22\n" : "not ok 22\n# `$_'\n"; | |
99 | ||
100 | # Are /^/m patterns scanned? | |
101 | $_ = join '|', split(/^aa/m, "aa b aa\naa d aa", 20); | |
102 | print $_ eq "| b aa\n| d aa" ? "ok 23\n" : "not ok 23\n# `$_'\n"; | |
103 | ||
104 | # Are /$/m patterns scanned? | |
105 | $_ = join '|', split(/aa$/m, "aa b aa\naa d aa", 20); | |
106 | print $_ eq "aa b |\naa d |" ? "ok 24\n" : "not ok 24\n# `$_'\n"; | |
107 | ||
108 | # Greedyness: | |
109 | $_ = "a : b :c: d"; | |
110 | @ary = split(/\s*:\s*/); | |
111 | if (($res = join(".",@ary)) eq "a.b.c.d") {print "ok 25\n";} else {print "not ok 25\n# res=`$res' != `a.b.c.d'\n";} | |
815d35b9 MG |
112 | |
113 | # use of match result as pattern (!) | |
114 | 'p:q:r:s' eq join ':', split('abc' =~ /b/, 'p1q1r1s') or print "no "; | |
115 | print "ok 26\n"; |