in reply to Re: Compare two strings of same length, character-by-character
in thread Compare two strings of same length, character-by-character
If all that is needed is a count of how many characters are the same, this can be reduced to:
#!/usr/bin/perl use strict; # https://www.perlmonks.org/?node_id=11155923 use warnings; my $str1='LFGSLSIIVAHHM'; my $str2='LFGSLSIIVSHHM'; use Algorithm::Diff qw(traverse_sequences); my $same = 0; traverse_sequences( [ split //, $str1 ], [ split //, $str2 ], { MATCH => sub { $same++ }, } ); print "$same characters are the same\n";
which outputs:
12 characters are the same
|
---|