in reply to Matching or masking specific characters in an array of strings
Here's a successive character-by-character approach that might work for you:
#!/usr/bin/perl -w use strict; my %groups = ( group1 => ['cooling', 'rooting', 'hooting', 'looking', 'doormat', +'cooking', 'cookies', 'noodles'], # desired result "-oo----" group2 => ['yourself', 'southern'], # desired result "-ou--e--" group3 => ['arden', 'arlen', 'asked'] # desired result "a--e-" ); my %matches; for my $group (keys %groups){ my $last; for my $word (@{$groups{$group}}){ if (!defined $last){ $last = $word; next; } else { die "Length mismatch ('$last'/'$word')\n" unless length($last) == length($word); my @chars = split //, $word; for (0..$#chars){ substr $last, $_, 1, "-" unless $chars[$_] eq substr $last, $_, 1; } } } $matches{$group} = $last; } print "$_: $matches{$_}\n" for sort keys %matches;
Output:
group1: -oo---- group2: -ou--e-- group3: a--e-
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Matching or masking specific characters in an array of strings
by duggles (Acolyte) on Dec 19, 2008 at 14:57 UTC |