home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.perl
- Path: sparky!uunet!ukma!bogus.sura.net!howland.reston.ans.net!usc!news.cerf.net!netlabs!lwall
- From: lwall@netlabs.com (Larry Wall)
- Subject: Re: split array assignment
- Message-ID: <1993Jan28.215138.15239@netlabs.com>
- Sender: news@netlabs.com
- Nntp-Posting-Host: scalpel.netlabs.com
- Organization: NetLabs, Inc.
- References: <1993Jan27.113301.2417@tpl68k0.tplrd.tpl.oz.au>
- Date: Thu, 28 Jan 1993 21:51:38 GMT
- Lines: 22
-
- In article <1993Jan27.113301.2417@tpl68k0.tplrd.tpl.oz.au> johne@tplrd.tpl.oz.au (John Eldred) writes:
- : Can some kind person tell me why this bit of code prints 4 3 ?
- :
- : $_ = "a b c\n";
- : $p = ($x, $y, $z) = split;
- : $q = split;
- : print $p, ' ', $q, "\n";
- :
- : It prints 3 3 if you chop the \n from $_.
-
- That's kinda complicated. The split operator deletes trailing null
- fields, like the null string following \n, (but only if there is no
- limit specified to the split). That's why $q ends up 3. $p ends
- up 4 for two reasons: first, the value of an assignment is the number
- of values that were *available* on the right-hand side, not the number
- that you ended up with on the left. Second, the split returned 4 values
- because an assignment of a split to a list automatically supplies a limit
- of one more than the number of items in the list. This is a useful
- optimization, and prevents split from doing a lot of extra work in many
- cases. However, it also has the effect you noticed.
-
- Larry
-