Path: utzoo!attcan!uunet!lll-winken!elroy.jpl.nasa.gov!jpl-devvax!lwall From: lwall@jpl-devvax.JPL.NASA.GOV (Larry Wall) Newsgroups: comp.lang.perl Subject: Re: an easier way? Message-ID: <10226@jpl-devvax.JPL.NASA.GOV> Date: 3 Nov 90 05:39:01 GMT References: <23043@fs2.NISC.SRI.COM> Reply-To: lwall@jpl-devvax.JPL.NASA.GOV (Larry Wall) Distribution: comp Organization: Jet Propulsion Laboratory, Pasadena, CA Lines: 46 In article <23043@fs2.NISC.SRI.COM> cwilson@NISC.SRI.COM (Chan Wilson [Animal]) writes: : I've got a script that distills mailserver stats down to a simple : access count. I've got the following snippet of code: : : $file{(reverse(split(/\//,$path)))[0]}++ if ($part =~ /A1\//); : : where $path is "/home/fs3/mserv/HELP" or somesuch. : : I'm curious - is there a better way to do this? I tried a number : of regexps, but couldn't come up with one, so had fun creating : the above one liner. :) That's pretty snazzy. You could avoid the split with a temp array: $file{@path = split(m#/#,$path), pop @path}++ if $part =~ m#A1/#; But it's not as beautificial as the reverse. To pull the last component of a pathname off with a regular expression, one can say things like: ($file) = $path =~ m#.*/(.*)#; ($file = $path) =~ s#.*/##; These both use a temp variable, of course, and don't just slip right into your expression easily, though you can use the comma operator: $file{($file) = $path =~ m#.*/(.*)#, $file}++ if $part =~ m#A1/#; $file{($file = $path) =~ s#.*/##, $file}++ if $part =~ m#A1/#; For the first one, you can get by without a temp var, which after all is only being used in a list to supply an array context, which can be provided other ways: $file{join('',$path =~ m#.*/(.*)#)}++ if $part =~ m#A1/#; or $file{($path =~ m#.*/(.*)#)[0]}++ if $part =~ m#A1/#; It might be better, however, to simply use rindex: $file{substr($path,rindex($path,'/')+1)}++ if $part =~ m#A1/#; I don't know if any of these are faster than your solution, though. Perl programming is an *empirical* science. :-) Larry