If you can use substr(), use it, because it works much faster than regexp. Teddy
----- Original Message ----- From: "Edward WIJAYA" <[EMAIL PROTECTED]> To: <beginners@perl.org> Sent: Sunday, January 02, 2005 5:08 PM Subject: Choosing between Regexp and Substr > Dear friends, > > After a couple of months dwelling into Perl scripting especially in > manipulating strings, I found myself resorting to use "substr" function a > lot. > > I had a feeling that the most of the "substr" function can be replaced > with regexp in any cases. For example the simple code below. I wonder how > would masters redo this function with pure reqexp approach. > > __BEGIN__ > sub hamming_distance_string{ > #String length is assumed to be equal > my ($a,$b) = @_; > my $len = length ($a); > my $num_match=0; > > for (my $i=0; $i<$len; $i++) { > ++$num_match if substr($a, $i, 1) eq substr($b, $i,1); > } > > return $num_match; > } > > __END__ > > Is it true that 'substr' function can be rewritten with regexp all the > time? If so when is it better to use which? Any rule of thumb? > > Hope to hear from you again. Thanks so much for your time. > And wishing you all a very Happy and Prosperous New Year 2005! -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] <http://learn.perl.org/> <http://learn.perl.org/first-response>