Chris Devers <[EMAIL PROTECTED]> writes:

> On Tue, 16 Sep 2003, Shevek wrote:
>
>> On Tue, 16 Sep 2003, Joel Bernstein wrote:
>>
>> > I have a problem with some code which I'm trying to debug. I'm not
>> > certain, but I think perhaps I'm doing something wrong in the
>> > following line - perhaps inadvertently creating an array slice?
>> >
>> > what do you understand by the line:
>> >    my $foo=( split ',' => $line )[7];
>> > ?
>>
>> I understand that you should have used two lines. Why not do so?
>>
>> my @tmp = split /,/, $line;
>> my $foo = $tmp[7];
>
> Fair enough, but what's a good, non-obfuscated-Perl one liner for this?
>
> Would this work?
>
>   my ( ,,,,,,$foo ) = ...
>
> No, that's ugly & brittle at best, and hopeless at worst.  Nevermind.
>
> I am curious about good idioms for doing an array slice like this
> though.

That's easy. The correct idiom is:

   my $foo = (split ',' => $line)[...]

With appropriate indices. This is *Perl* for fuck's sake, not C or
Java. Indexing into a list is a perfectly sensible thing to do.

Reply via email to