Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!samsung!sol.ctr.columbia.edu!src.honeywell.com!msi.umn.edu!cs.umn.edu!cybrspc!roy From: roy%cybrspc@cs.umn.edu (Roy M. Silvernail) Newsgroups: comp.lang.perl Subject: Re: $# question Message-ID: <69oys1w163w@cybrspc> Date: 21 Nov 90 01:13:28 GMT References: <108982@convex.convex.com> Organization: Villa CyberSpace, Minneapolis, MN Lines: 41 tchrist@convex.COM (Tom Christiansen) writes: > It's also wrong -- you're using @ for $, which is a no-no, and tended > to coredump earlier version of perl. And I learn Yet Another Perl of Wisdom... ;-) I'm going to learn this language yet! > 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. Just to cover all bases, then... is there a preferred Perl Way to access the last element in an array, besides $foo[$#foo]? > 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. My application reads , processes each line separately, and (if the line qualifies) appends to an array. Thus, the push() call is just what I needed. Thanks, Tom, for your guidance. Until 'The Book' comes out, this newsgroup is my greatest asset in learning Perl. -- Roy M. Silvernail |+| roy%cybrspc@cs.umn.edu |+| #define opinions ALL_MINE; main(){float x=1;x=x/50;printf("It's only $%.2f, but it's my $%.2f!\n",x,x);} "This is cyberspace." -- Peter da Silva :--: "...and I like it here!" -- me