<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD><TITLE>Message</TITLE>
<META content="text/html; charset=iso-8859-1" http-equiv=Content-Type>
<META content="MSHTML 5.00.3539.2400" name=GENERATOR></HEAD>
<BODY>
<DIV> </DIV>
<BLOCKQUOTE style="MARGIN-RIGHT: 0px">
<DIV></DIV>
<DIV><TT><FONT face=Arial size=2>> On 6/28/05, Bill Campbell
<bill@celestial.com> wrote:<BR>> > On Tue, Jun 28, 2005, Uri
London wrote:<BR>> > ><BR>> > ><BR>> > >
What is the idiomatic way to extract first/last item after split?<BR>> >
><BR>> > > More interestingly, why #2 doesn't work, while
#3 does?<BR>> > <BR>> > If I want the first and last items from a
split, I would probably do <BR>> > it something like:<BR>> >
<BR>> > my ($first, @rest) =
split(...);<BR>> > my $last =
pop(@rest);<BR>> ><BR>> <BR>> >> this might not work
as one expects on a list of one, since @rest <BR>> will be empty. ($last
will contain undef <BR>> >>after the pop.) it's unclear from the
original poster's <BR>> requirements what this edge case should
return.<BR>> <BR>> True and the same thing applies to J. Krahn's elegant
solution. Once <BR>> the output's drained, the rest of the list will be
undefined.<BR>> <BR>> ($first,$last) = (split ...)[0,-1]; #
$last undefined if list of 1<BR>> <BR></FONT></TT><FONT
face=Arial><FONT size=2><TT><SPAN class=629412318-29062005><FONT color=#0000ff
face=Arial> <FONT color=#000000>>></FONT> </FONT></SPAN>Not
true, the slice "[0,-1]" does not "drain", but just reuses the same
element <SPAN class=629412318-29062005><FONT color=#0000ff
face=Arial> </FONT></SPAN></TT></FONT></FONT></DIV>
<DIV><FONT face=Arial><FONT size=2><TT><SPAN class=629412318-29062005><FONT
color=#0000ff face=Arial>>></FONT> </SPAN>in the case of one
element after the <SPAN class=629412318-29062005><FONT color=#0000ff
face=Arial> </FONT></SPAN>split. <SPAN
class=629412318-29062005><FONT color=#0000ff
face=Arial> </FONT></SPAN>This then creates the
appropriate <SPAN class=629412318-29062005><FONT color=#0000ff
face=Arial> </FONT></SPAN></TT></FONT></FONT></DIV>
<DIV><FONT face=Arial><FONT size=2><TT><SPAN class=629412318-29062005><FONT
color=#0000ff face=Arial>>></FONT> </SPAN>2 elements to initialize
$first and $last. Of course, $string must be initialized <SPAN
class=629412318-29062005><FONT color=#0000ff
face=Arial> </FONT></SPAN></TT></FONT></FONT></DIV>
<DIV><FONT face=Arial><FONT size=2><TT><SPAN class=629412318-29062005><FONT
color=#0000ff>>></FONT> </SPAN>to something other than what it
would split on</TT></FONT><FONT size=2><TT>my $string =
"one";</TT></FONT> <BR><FONT size=2><TT><SPAN
class=629412318-29062005><FONT color=#0000ff> >></FONT></SPAN><FONT
color=#0000ff> </FONT> my ($first,$last) = (split /\s+/,
$string)[0,-1];</TT></FONT> <BR><FONT size=2><TT> <SPAN
class=629412318-29062005><FONT color=#0000ff>>> </FONT></SPAN>
print "$first\n";</TT></FONT> <BR><FONT size=2><TT> <SPAN
class=629412318-29062005> <FONT
color=#0000ff> >></FONT></SPAN><FONT
color=#0000ff> </FONT> print "$last\n";</TT></FONT> <FONT
size=2><FONT color=#0000ff><SPAN
class=629412318-29062005> </SPAN></FONT></FONT></FONT></DIV>
<DIV> </DIV>
<DIV><FONT face=Arial><FONT size=2><FONT color=#0000ff><SPAN
class=629412318-29062005>True, but that's very much semantic overkill in my
opinion. I'd have to argue that the result is
</SPAN></FONT></FONT></FONT></DIV>
<DIV><FONT face=Arial><FONT size=2><FONT color=#0000ff><SPAN
class=629412318-29062005>certainly the same
as a</SPAN></FONT></FONT></FONT><FONT face=Arial><FONT size=2><FONT
color=#0000ff><SPAN class=629412318-29062005> "drain" even ,if technically,
split is orchestrating the effect.</SPAN></FONT></FONT></FONT></DIV>
<DIV> </DIV>
<DIV><FONT face=Arial><FONT size=2><FONT color=#0000ff><SPAN
class=629412318-29062005></SPAN></FONT></FONT></FONT> </DIV>
<DIV><FONT face=Arial><FONT size=2><FONT color=#0000ff><SPAN
class=629412318-29062005>--</SPAN></FONT></FONT></FONT></DIV>
<DIV><FONT face=Arial><FONT size=2><FONT color=#0000ff><SPAN
class=629412318-29062005>Charles
DeRykus</SPAN></FONT></FONT></FONT></DIV></BLOCKQUOTE></BODY></HTML>