SPUG: spug: What is the idiomatic way to extract first/last item after split?

jerry gay jerry.gay at gmail.com
Tue Jun 28 21:54:37 PDT 2005


On 6/28/05, Bill Campbell <bill at celestial.com> wrote:
> On Tue, Jun 28, 2005, Uri London wrote:
> >
> >
> >   What is the idiomatic way to extract first/last item after split?
> >
> >   More interestingly, why #2 doesn't work, while #3 does?
> 
> If I want the first and last items from a split, I would probably do it
> something like:
> 
>        my ($first, @rest) = split(...);
>        my $last = pop(@rest);
> 
this might not work as one expects on a list of one, since @rest will
be empty. ($last will contain undef after the pop.) it's unclear from
the original poster's requirements what this edge case should return.

~jerry


More information about the spug-list mailing list