Path: utzoo!utgpu!news-server.csri.toronto.edu!bonnie.concordia.ca!thunder.mcrcim.mcgill.edu!snorkelwacker.mit.edu!usc!cs.utexas.edu!convex!news From: tchrist@convex.COM (Tom Christiansen) Newsgroups: comp.lang.perl Subject: Re: what should length(@array) do ? Message-ID: <1991Jan10.025554.16925@convex.com> Date: 10 Jan 91 02:55:54 GMT References: Sender: news@convex.com (news access account) Reply-To: tchrist@convex.COM (Tom Christiansen) Organization: CONVEX Software Development, Richardson, TX Lines: 38 Nntp-Posting-Host: pixel.convex.com From the keyboard of emv@ox.com (Ed Vielmetti): :I had some code that looked like :body: { : @obody = ; :} :which seemed to work just fine and quick too. Now I want to find out :how many bytes are in this array. The obvious (?) solution to me was : $obodylen = length(@obody); :but that gets me "3", probably the size of some symbol table entry. Remembering that @foo in a scalar context yields the number of elements, I'll bet you that you read in between 100 and 999 elements in the array. :The solution I now have does :body: while () { : push(@obody,$_) ; : $obodylen += length($_); :} :seems to work fine, but in the tradition of perl hacking, there's :probably a better way. A better way? No, but in the tradition of perl hacking there are a googol of *other* ways. :-) The way you have there looks pretty good, since you've got each line to grab the length of as you're going. If I had to write an array length function the way you've described it, I'd probably use something like: sub alen { # usage: $n = &alen(*ary); where @ary exists local(*a) = @_; local($len,$_); grep($len += length, @a); $len; } --tom