in reply to Re: split'ing lines with variable number of fields
in thread split'ing lines with variable number of fields

Yes, you are right about the extra brackets. But the rest is more complicated.
my ($command, $first, $second) = (split) [0, 1, -1]; $second = $first unless defined $second;
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.

With the development version of Perl, you could just do:

my ($command, $first, $second) = (split) [0, 1, -1];
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.

Luckely, the line

$second = $first unless defined $second;
won't harm you in the development version, as $second will be defined, so no assignment is done.

-- Abigail

Replies are listed 'Best First'.
Re: Re: split'ing lines with variable number of fields
by IraTarball (Monk) on Jun 20, 2001 at 02:33 UTC
    Exactly what I wanted to know.

    Thanks!!!
    Ira

    "So... What do all these little arrows mean?"
    ~unknown