Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!uunet!rbj From: rbj@uunet.uu.net (Root Boy Jim) Newsgroups: comp.lang.perl Subject: Re: Counting RE occurrences Message-ID: <1991May16.010149.20536@uunet.uu.net> Date: 16 May 91 01:01:49 GMT References: <1991May13.184504.13844@demon.co.uk> <1991May13.225603.29819@convex.com> Organization: UUNET Communications Services, Falls Church, VA Lines: 56 tchrist@convex.COM (Tom Christiansen) writes: >From the keyboard of Paul Moore : >:This is one of those problems which I am convinced ought to have a simple >:(probably one-line) solution in perl, but I sure can't find it... Truly. >:I have a string, which contains a piece of text. I also have a regular >:expression. I want to count the number of times the RE appears in the >:string. Quite simply, the answer is: split(/RE/,exp) - 1; I don't know why Tom missed the easy answer after giving the hard ones. >:As an example (this is the task which first made me want to do this), I >:have a file, which has been copied from an MS-DOS box to my (non-MS-DOS) >:machine. So the lines in the file are delimited by "\r\n", and not just >:"\n". I have slurped the file into a string, in order to do some processing, >:and I need to count the number of lines. So what I want to do is count the >:number of occurrences of the string "\r\n" in the string. > > I don't know what else you're doing, but I would think that slurping > is a pretty inefficient way. I usually try to avoid it. It sure does > make some things easier, though. You don't have to slurp the whole file. Just set $/ to say, a space, or anything other than \r or \n. With a bit of memory you could also use read or sysread. Remember to paste trailing \r's onto the beginning of the next block. >I know, I know... along that road lies APL and madness. Too late. Perl is already weirder than APL. Uglier too. APL is mathematically pure. Perl is engineering and computer science at warp speed. APL handles arrays of arbitrary dimensions. Perl's objects are only one dimension, but may be associative. APL has no operator precedence (I consider this a plus), but is weak on control flow. Both have numeric and character data types, but Perl has regexps and common string operators builtin. Perl also interfaces to the operating system better, most likely because it was designed on a reasonable one. The fact that both languages inspire one-liners (and thos who write them :-) is perhaps their greatest common feature. -- [rbj@uunet 1] stty sane unknown mode: sane