in reply to Comparing string characters
G'day dnamonk,
Code:
#!/usr/bin/env perl use strict; use warnings; my @tests = ( [qw{ABCGE ABCGE}], [qw{ABCGE FGCGB}], [qw{ABCGE JHAGT}], ); for my $strings (@tests) { my $diff = 0; for my $i (0 .. length($strings->[0]) - 1) { my @chars = map substr($strings->[$_], $i, 1), 0, 1; ++$diff if $chars[0] ne $chars[1]; } print "@$strings $diff\n"; }
Output:
ABCGE ABCGE 0 ABCGE FGCGB 3 ABCGE JHAGT 4
You've received a number of solutions; use Benchmark to see which is the most efficient. From your username, I'm guessing you're dealing with biological data: typically huge and efficiency is usually important.
— Ken
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Comparing string characters
by Marshall (Canon) on Nov 23, 2021 at 18:56 UTC |