SPUG: spug: What is the idiomatic way toextractfirst/lastitemafter split?

Rick Croote rick.croote at philips.com
Thu Jun 30 11:09:52 PDT 2005

We are having way too much fun with this. Good comment on commenting 
Brian!  Yitzchak has appropriately responded for me.  But I think the term 
"drain", that I did not use first, but, none the less has now been 
twisted.  The first use of the word "drain" in the context of this 
discussion came from the shift/pop solution, where the array was 
prematurely "drain"ed from the shift, leaving nothing to pop, causing 
$last to be undefined.  So the term drain was someone else used to 
describe the emptying of an array through the use of shift and pop.  I 
pointed out that the slice solution does not drain anything because it 
merely accesses two elements, nondestructively to the array, which in this 
case was anonymous and lost anyway.  The split, as others have pointed 
out, had nothing to do with it, as it only created the array from a 
string, and was not being used directly to populate $first and $last.

Rick Croote
Software Engineer
Environment and Tools Team
Philips Medical Systems
Bothell, WA
Rick.Croote at Philips.com
Phone: 425-487-7834

Yitzchak Scott-Thoennes 
Sent by:
spug-list-bounces at pm.org
2005-06-30 10:49 AM

"DeRykus, Charles E" <charles.e.derykus at boeing.com>
spug-list at mail.pm.org
Re: SPUG: spug: What is the idiomatic way toextractfirst/lastitemafter 

On Thu, Jun 30, 2005 at 09:28:06AM -0700, DeRykus, Charles E wrote:
> Ok, I characterized this as a "drain" because, superficially at
> least, you're filling a bigger bucket with a smaller one.  The split
> output consists of a single item but the Action of the function
> split populates $first with that single item; and then backfills
> $last with an undef. Here's what I assume Rick was referring to in
> claiming that no "drain" occurred:
>      perdoc -f split:
>      When assigning to a list, if LIMIT is omitted, Perl supplies a 
>      one larger than the number of variables in the list, to avoid
>      unnecessary work. 
>      perl -MO=Deparse -e '($first,$last) = split(" ","foo");'
>      ($first, $last) = split(" ", 'foo', 3);
> The split really generates only 1 element; behind the scenes, split
> orchestrates output so $first gets populated with that 1 element and
> then $last gets backfilled with an 'undef' because the original list
> is exhausted. Maybe that seat-of-the-pants explanation is flawed
> somehow...
> I assume Rick felt that because split generates values for both
> $first and $last this shouldn't be thought of as a "drain". To me,
> it just seems natural to call it a "drain" because there's really a
> 1 item output and that's trying to fill 2 slots.

But the case under consideration isn't ($first, $last) = split ...;,
it's ($first, $last) = ( split ... )[0, -1]; which indeed never leaves
$last undefined.  If only one element is produced by split, both
$first and $last get assigned that value.
Seattle Perl Users Group Mailing List 
     POST TO: spug-list at pm.org
SUBSCRIPTION: http://mail.pm.org/mailman/listinfo/spug-list
    MEETINGS: 3rd Tuesdays, Location: Amazon.com Pac-Med
    WEB PAGE: http://seattleperl.org/

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.pm.org/pipermail/spug-list/attachments/20050630/7cd79e1e/attachment.html

More information about the spug-list mailing list