will only "work" because of a bug in Perl. It's still present in perl5.6.1, however, it's fixed in the development release. Index -1 should always be the last element. Regardless of how many elements there are in the list. The bug, however, is that when indexing over the result of a split, you cannot "reuse" elements. Repeating indexes result in undef. And when there are only two elements in the split, 1 and -1 will point to the same element - making $second undefined.my ($command, $first, $second) = (split) [0, 1, -1]; $second = $first unless defined $second;
With the development version of Perl, you could just do:
and $second will be the right thing: the last element of the split. Hence, if there are just two items, $second will be the same as $first, just what you want.my ($command, $first, $second) = (split) [0, 1, -1];
Luckely, the line
won't harm you in the development version, as $second will be defined, so no assignment is done.$second = $first unless defined $second;
-- Abigail
In reply to Re: split'ing lines with variable number of fields
by Abigail
in thread split'ing lines with variable number of fields
by IraTarball
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |