in reply to If there a way to find the location of the first difference between two strings?

Untested:
use 5.010; my $t = $s1 ^ $s2; my ($f) = $t =~ /^(\x{00}*)/; say length $f;
  • Comment on Re: If there a way to find the location of the first difference between two strings?
  • Download Code

Replies are listed 'Best First'.
Re^2: If there a way to find the location of the first difference between two strings?
by jwkrahn (Abbot) on Mar 26, 2012 at 09:33 UTC
    use 5.010; my $t = $s1 ^ $s2; my ($f) = $t =~ /^(\x{00}*)/; say length $f;

    Another way to do that:

    use 5.010; my $t = $s1 ^ $s2; $t =~ /^\0*/ && say $+[0]

      JavaFan and jwkrahn,

      C o o l !

      That's the code I was hoping for...

      Need to benchmark now!

      UPDATE: My apologies to JavaFan, I didn't realize his post was first. Both solutions Benchmarked are better than mine (376%).

      Thank you

      "Well done is better than well said." - Benjamin Franklin