3 ms·
I want to preface this with please don't write Perl like this, and don't blame it on the language. But anyway, there are multiple issues. One is that, as other
by perl4ever 8y ago
I want to preface this with please don't write Perl like this, and don't blame it on the language.
But anyway, there are multiple issues. One is that, as other comments point out, all the results are going to the leftmost variable.
But the other is indeed fixable with parentheses. The code provided outputs two items when it presumably should give four.
my (@arr3, @arr4);
(@arr3[0..1],@arr4[0..1]) = (index($str2, '-') != -1) ? (split (/-/,$str2), split (/;/, $str4)) : (split (/:/,$str2), split (/;/, $str4));
array 3[0] is: This string has two sentences
array 3[1] is: A dash separates them
array 4[0] is: This string has different punctuation
array 4[1] is: a semicolon
- simcop2387 8y agoYea, using slices can accomplish this too. But if you're going to be using slices or a fixed number of elements on the left hand side you should also be using the third argument to split($delim, $input, $count) to tell split to only produce so many outputs. If the input string would split into more than two pieces in your original code you would end up overflowing into @arr4 and losing results from the second split.
- perl4ever 8y agoMaking it robust or sensible presumes there's some sort of coherent intent behind it, which was already disclaimed in the blog post.