home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1993 #3 / NN_1993_3.iso / spool / comp / lang / perl / 8034 < prev    next >
Encoding:
Text File  |  1993-01-28  |  1.4 KB  |  35 lines

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