Path: utzoo!utgpu!watserv1!watmath!att!att!linac!pacific.mps.ohio-state.edu!zaphod.mps.ohio-state.edu!usc!cs.utexas.edu!sun-barr!lll-winken!uunet!convex!convex.COM From: tchrist@convex.COM (Tom Christiansen) Newsgroups: comp.lang.perl Subject: Re: $# question Message-ID: <108982@convex.convex.com> Date: 19 Nov 90 23:05:29 GMT References: Sender: news@convex.com Reply-To: tchrist@convex.COM (Tom Christiansen) Organization: CONVEX Software Development, Richardson, TX Lines: 54 In article roy%cybrspc@cs.umn.edu (Roy M. Silvernail) writes: >I'm a little confused about a detail regarding $#... > >Wouldn't $# make more sense if it were to >point to the array element _past_ the last element assigned? (i.e. to >the null element past '5' in the above example) Then it could be used >directly as an indication of size, and as well, you could do > >while () { > @foo[$#foo] = $_; >} > >to fill an array with an incoming stream of fields. (using @foo[$#foo +1] >works, but it's less intuitive to me) It's also wrong -- you're using @ for $, which is a no-no, and tended to coredump earlier version of perl. I used to get bug reports on this one from my users all the time. Remember: when you want ONE value, use a $, and when you want many, use an @, as in: print $foo[1]; print @foo[1..3]; Anyway, while you could have written the above as: while () { $foo[++$#foo] = $_; } That's not idiomatic perl. In <8225@jpl-devvax.JPL.NASA.GOV> Larry wrote: (yes, I do collect these.) Whenever you see subscripts in a Perl script, it's a pretty strong indication that things aren't being done the Perl Way. A cheaper way way to do this in perl would be while () { push(@a, $_); } This runs in 70% the time of the previous example on my machine. This is nearly as fast as @a = ; Which runs in 65% of the original time. Of course, you may not want to do this on small-memory machines and/or big files. --tom