Indexing directly into returned array in Perl

前端 未结 2 841
囚心锁ツ
囚心锁ツ 2021-01-01 17:38

This question has been asked about PHP both here and here, and I have the same question for Perl. Given a function that returns a list, is there any way (or what is the best

2条回答
  •  被撕碎了的回忆
    2021-01-01 18:25

    Just like you can do this:

    ($a, $b, $c) = @array;
    

    You can do this:

    my($a) = split /,/, $comma_separated;
    

    my $a on the LHS (left hand side) is treated as scalar context. my($a) is list context. Its a single element list so it gets just the first element returned from split.

    It has the added benefit of auto-limiting the split, so there's no wasted work if $comma_separated is large.

提交回复
热议问题