Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!know!zaphod.mps.ohio-state.edu!usc!elroy.jpl.nasa.gov!jpl-devvax!lwall From: lwall@jpl-devvax.JPL.NASA.GOV (Larry Wall) Newsgroups: comp.lang.perl Subject: Re: Counting characters with unix utilities Message-ID: <9682@jpl-devvax.JPL.NASA.GOV> Date: 26 Sep 90 18:21:45 GMT References: <1990Sep26.125033.19669@uvaarpa.Virginia.EDU> Reply-To: lwall@jpl-devvax.JPL.NASA.GOV (Larry Wall) Organization: Jet Propulsion Laboratory, Pasadena, CA Lines: 36 In article <1990Sep26.125033.19669@uvaarpa.Virginia.EDU> worley@compass.com writes: : : From: merlyn@iwarp.intel.com (Randal Schwartz) : : | perl -ne '$c += tr/A/A/; print $c if eof;' file : | : | Doesn't this fail if the file is empty? : : Ooops. I'll be out painting fence-posts for a while, sorry. :-) : : It seems like it would be useful to be able to prescribe prolog and : epilog code for -n (and -p) loops. AWK does this with BEGIN and END. : Then it would be simple: : : perl -ne '$c += tr/A/A/' -xxx 'print $c' file It hardly seems worth it to add a new switch when you can say perl -e 'while(<>){$c += tr/A/A/;}print $c' file or perl -ne '$c += tr/A/A/; print $c if eof()' file or perl -e 'undef $/; $_ = <>; print tr/A/A/' file or even perl -e '$c += tr/A/A/ while <>; print $c' file Although the Perl Slogan is There's More Than One Way to Do It, I hesitate to make 10 ways to do something. :-) Larry