Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!samsung!sdd.hp.com!zaphod.mps.ohio-state.edu!julius.cs.uiuc.edu!apple!sun-barr!newstop!texsun!convex!convex.COM From: tchrist@convex.COM (Tom Christiansen) Newsgroups: comp.lang.perl Subject: Re: $# question Message-ID: <109132@convex.convex.com> Date: 21 Nov 90 23:00:32 GMT References: <108982@convex.convex.com> <69oys1w163w@cybrspc> Sender: news@convex.com Reply-To: tchrist@convex.COM (Tom Christiansen) Organization: CONVEX Software Development, Richardson, TX Lines: 26 In article <69oys1w163w@cybrspc> roy%cybrspc@cs.umn.edu (Roy M. Silvernail) writes: |tchrist@convex.COM (Tom Christiansen) writes: |> 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]? If you just want to add or remove it, push and pop are pretty quick. If you want to muck with it in place, $foo[$#foo] is fine. Just don't do this: for ($i = $[; $i <= $#a; $i++) { $a[$i] = .... } when this is there begging to be used: foreach (@a) { # now access each element (by reference) as $_ # which is usually implicit anyway } --tom