<!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>&nbsp;</DIV>
<BLOCKQUOTE style="MARGIN-RIGHT: 0px">
  <DIV></DIV>
  <DIV><TT><FONT face=Arial size=2>&gt; On 6/28/05, Bill Campbell 
  &lt;bill@celestial.com&gt; wrote:<BR>&gt; &gt; On Tue, Jun 28, 2005, Uri 
  London wrote:<BR>&gt; &gt; &gt;<BR>&gt; &gt; &gt;<BR>&gt; &gt; &gt; &nbsp; 
  What is the idiomatic way to extract first/last item after split?<BR>&gt; &gt; 
  &gt;<BR>&gt; &gt; &gt; &nbsp; More interestingly, why #2 doesn't work, while 
  #3 does?<BR>&gt; &gt; <BR>&gt; &gt; If I want the first and last items from a 
  split, I would probably do <BR>&gt; &gt; it something like:<BR>&gt; &gt; 
  <BR>&gt; &gt; &nbsp; &nbsp; &nbsp; &nbsp;my ($first, @rest) = 
  split(...);<BR>&gt; &gt; &nbsp; &nbsp; &nbsp; &nbsp;my $last = 
  pop(@rest);<BR>&gt; &gt;<BR>&gt; &nbsp;<BR>&gt; &gt;&gt; this might not work 
  as one expects on a list of one, since @rest <BR>&gt; will be empty. ($last 
  will contain undef <BR>&gt; &gt;&gt;after the pop.) it's unclear from the 
  original poster's <BR>&gt; requirements what this edge case should 
  return.<BR>&gt; <BR>&gt; True and the same thing applies to J. Krahn's elegant 
  solution. Once <BR>&gt; the output's drained, the rest of the list will be 
  undefined.<BR>&gt; <BR>&gt; ($first,$last) = (split ...)[0,-1]; &nbsp; &nbsp;# 
  $last undefined if list of 1<BR>&gt;&nbsp;<BR></FONT></TT><FONT 
  face=Arial><FONT size=2><TT><SPAN class=629412318-29062005><FONT color=#0000ff 
  face=Arial>&nbsp;<FONT color=#000000>&gt;&gt;</FONT>&nbsp;</FONT></SPAN>Not 
  true, the slice "[0,-1]" does not "drain", but just reuses the same 
  element&nbsp;<SPAN class=629412318-29062005><FONT color=#0000ff 
  face=Arial>&nbsp;</FONT></SPAN></TT></FONT></FONT></DIV>
  <DIV><FONT face=Arial><FONT size=2><TT><SPAN class=629412318-29062005><FONT 
  color=#0000ff face=Arial>&gt;&gt;</FONT>&nbsp;</SPAN>in the case of one 
  element after the&nbsp;<SPAN class=629412318-29062005><FONT color=#0000ff 
  face=Arial>&nbsp;&nbsp;</FONT></SPAN>split.&nbsp;&nbsp;<SPAN 
  class=629412318-29062005><FONT color=#0000ff 
  face=Arial>&nbsp;&nbsp;</FONT></SPAN>This then creates the 
  appropriate&nbsp;<SPAN class=629412318-29062005><FONT color=#0000ff 
  face=Arial>&nbsp;</FONT></SPAN></TT></FONT></FONT></DIV>
  <DIV><FONT face=Arial><FONT size=2><TT><SPAN class=629412318-29062005><FONT 
  color=#0000ff face=Arial>&gt;&gt;</FONT>&nbsp;</SPAN>2 elements to initialize 
  $first and $last. &nbsp;Of course, $string must be initialized&nbsp;<SPAN 
  class=629412318-29062005><FONT color=#0000ff 
  face=Arial>&nbsp;</FONT></SPAN></TT></FONT></FONT></DIV>
  <DIV><FONT face=Arial><FONT size=2><TT><SPAN class=629412318-29062005><FONT 
  color=#0000ff>&gt;&gt;</FONT>&nbsp;</SPAN>to something other than what it 
  would split on</TT></FONT><FONT size=2><TT>my $string = 
  "one";</TT></FONT>&nbsp;<BR><FONT size=2><TT><SPAN 
  class=629412318-29062005><FONT color=#0000ff>&nbsp;&gt;&gt;</FONT></SPAN><FONT 
  color=#0000ff>&nbsp;</FONT>&nbsp; my ($first,$last) = (split /\s+/, 
  $string)[0,-1];</TT></FONT>&nbsp;<BR><FONT size=2><TT>&nbsp;<SPAN 
  class=629412318-29062005><FONT color=#0000ff>&gt;&gt;&nbsp;</FONT></SPAN> 
  print "$first\n";</TT></FONT>&nbsp;<BR><FONT size=2><TT>&nbsp;<SPAN 
  class=629412318-29062005>&nbsp;<FONT 
  color=#0000ff>&nbsp;&gt;&gt;</FONT></SPAN><FONT 
  color=#0000ff>&nbsp;</FONT>&nbsp; print "$last\n";</TT></FONT>&nbsp;<FONT 
  size=2><FONT color=#0000ff><SPAN 
  class=629412318-29062005>&nbsp;</SPAN></FONT></FONT></FONT></DIV>
  <DIV>&nbsp;</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&nbsp;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&nbsp;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>&nbsp;</DIV>
  <DIV><FONT face=Arial><FONT size=2><FONT color=#0000ff><SPAN 
  class=629412318-29062005></SPAN></FONT></FONT></FONT>&nbsp;</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>