Newsgroups: comp.lang.perl Path: utzoo!utgpu!news-server.csri.toronto.edu!rpi!zaphod.mps.ohio-state.edu!wuarchive!uunet!convex!usenet From: Tom Christiansen Subject: Re: two perl questions Message-ID: <1991Jun08.060025.21067@convex.com> Sender: usenet@convex.com (news access account) Nntp-Posting-Host: pixel.convex.com Reply-To: tchrist@convex.COM (Tom Christiansen) Organization: CONVEX Software Development, Richardson, TX References: <1991Jun7.163638.7834@iWarp.intel.com> <$&bHr-=p@cs.psu.edu> Date: Sat, 08 Jun 1991 06:00:25 GMT Lines: 127 Felix Lee wrote, quoting Randal: :>Array can be forced by putting it into a list, or by assigning into :>an array. : :"putting it into a list" is not quite enough. Perl has the real :problem that lists are indistinguishable from the comma operator. : $x = (5, 10); : print "$x\n"; : $x = @x = (5, 10); : print "$x\n"; Quite so; that's is why I said ``put it in a list AND SUBSCRIPT IT.'' :What if you want the last word from a split()? This: : $x = split(' ', 'a b c'); :returns the number of split items. This: : @x = split(' ', 'a b c'); : $x = $x[$#x]; :works, but requires a temporary array. This requires no *named* temporary array: $x = (reverse split(' ','a b c'))[0]; Although to pull out the last piece of something, I'd use a regexp anyway: ($x) = 'a b c' =~ /(\S+)$/; $x = ('a b c' =~ /(\S+)$/)[0]; :Do you think this works? : $x = (0, split(' ', 'a b c')); Yes. Don't you? Shouldn't it? :How about this? : sub i1 { @_; } : $x = &i1(split(' ', 'a b c')); :And this? : sub i2 { return @_; } : $x = &i2(split(' ', 'a b c')); :Do you understand the difference between &i1 and &i2? I certainly :don't. So, &i1 yields 3, whereas &i2 yield 'c'. I won't claim to really understand it, but here are some clues. If you compile with -D1024, you'll find that the assignments generate the same code, but the two subroutines are very different: that 'return' incurs a good deal of overhead, just enough to make it do what I'm assuming you want it to. SUB main'i1 = { C_TYPE = EXPR C_ADDR = 0x800957c8 C_NEXT = 0x0 C_LINE = 8 (0x800957c8) C_OPT = CFT_EVAL C_FLAGS = (COND,TERM) C_EXPR = { OP_TYPE = ARRAY OP_LEN = 1 [1]ARG_TYPE = STAB (unevaluated) [1]ARG_STAB = { STAB_NAME = main'_ } } AC_STAB = NULL AC_EXPR = NULL } But here's the 2nd. SUB main'i2 = { C_TYPE = BLOCK C_ADDR = 0x80095b88 C_NEXT = 0x0 C_LINE = 9 (0x80095b88) C_LABEL = "_SUB_" C_OPT = CFT_FALSE C_FLAGS = (TERM) C_EXPR = NULL CC_TRUE = { C_TYPE = EXPR C_ADDR = 0x80095948 C_NEXT = 0x0 C_LINE = 9 (0x80095948) C_OPT = CFT_EVAL C_FLAGS = (COND,TERM) C_EXPR = { OP_TYPE = RETURN OP_LEN = 2 [1]ARG_TYPE = WORD (unevaluated) [1]ARG_STAB = {} [2]ARG_TYPE = EXPR [2]ARG_FLAGS = (ARYOK) [2]ARG_ARG = { OP_TYPE = ARRAY OP_LEN = 1 OP_FLAGS = (LISTISH) [1]ARG_TYPE = STAB (unevaluated) [1]ARG_STAB = { STAB_NAME = main'_ } } } AC_STAB = NULL AC_EXPR = NULL } CC_ALT = NULL } Notice in particular how the ARG_FLAGS in &i2 are ARYOK, and also &i2's OP_FLAGS are LISTISH. You don't see that in &i1. But I'm not particularly happy with this. Larry, you must say that this behavior is at the very least unexpected, eh? :Let's hear it for experimental programming. Perl programming is an *empirical* science. :-) Larry Wall in <10226@jpl-devvax.JPL.NASA.GOV> --tom -- Tom Christiansen tchrist@convex.com convex!tchrist "So much mail, so little time."