in reply to Comparing string characters

> easiest way to compare substrings

use v5.12; use warnings; my $x ='ABCGE ABCGE'; my $y ='ABCGE FGCGB'; my $z ='ABCGE JHAGT'; say scalar ($x ^ $y) =~ tr/\0//c; say scalar ($x ^ $z) =~ tr/\0//c;

3 4

edit

Oh I probably misread your examples, but if you compare first and second column you'll get the same results.

update

covering both cases

use v5.12; use warnings; use Data::Dump qw/pp dd/; use Test::More; sub distance { my ( $x,$y ) = @_ ; return scalar (("$x" ^ "$y") =~ tr/\0//c ); } my $x ='ABCGE ABCGE'; my $y ='ABCGE FGCGB'; my $z ='ABCGE JHAGT'; is( distance($x,$y) => 3, "by row"); is( distance($x,$z) => 4, "by row"); while (<DATA>){ my @col = split; is( distance($col[0],$col[1]) => $col[2],"by col"); } done_testing; __DATA__ ABCGE ABCGE 0 ABCGE FGCGB 3 ABCGE JHAGT 4

Cheers Rolf
(addicted to the Perl Programming Language :)
Wikisyntax for the Monastery

Replies are listed 'Best First'.
Re^2: Comparing string characters
by dnamonk (Acolyte) on Nov 22, 2021 at 18:20 UTC
    Thanks a lot. That was really helpful :)