<br><font size=2><tt><br>
&gt; <br>
&gt; <br>
&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; <br>
&gt; --<br>
&gt; Charles DeRykus<br>
</tt></font>
<br><font size=2><tt>Not true, the slice &quot;[0,-1]&quot; does not &quot;drain&quot;,
but just reuses the same element in the case of one element after the split.
&nbsp;This then creates the appropriate 2 elements to initialize $first
and $last. &nbsp;Of course, $string must be initialized to something other
than what it would split on. &nbsp;I lost track of who posted this solution,
you say J. Krahn, I say, very nice solution J. Krahn. &nbsp;You gotta love
elegant one line solutions :)</tt></font>
<br>
<br><font size=2><tt>&nbsp; &nbsp; my $string = &quot;one&quot;;</tt></font>
<br><font size=2><tt>&nbsp; &nbsp; my ($first,$last) = (split /\s+/, $string)[0,-1];</tt></font>
<br><font size=2><tt>&nbsp; &nbsp; print &quot;$first\n&quot;;</tt></font>
<br><font size=2><tt>&nbsp; &nbsp; print &quot;$last\n&quot;;</tt></font>
<br>
<br>