# In this example, 'first' column are neighbours with only 1 position differ. The rest of the columns are 2 positions differ Seed = 000 100 110 120 130 101 102 103 200 210 220 230 201 202 203 300 310 320 330 301 302 303 010 011 012 013 020 021 022 023 030 031 032 033 001 002 003 Seed = 001 101 111 121 131 100 102 103 201 211 221 231 200 202 203 301 311 321 331 300 302 303 011 010 012 013 021 020 022 023 031 030 032 033 000 003 002 Hence given a tag of length L we will have 3*L + 9L(L-1)/2 neighbors #### #!/usr/bin/perl -w use strict; use Data::Dumper; use Carp; # If possible we prefer not to use any CPAN module # or regex. It's for my understanding my $seed = $ARGV[0] || "000"; # Seed can be longer than 3 digits but always 0,1,2,3 for (my $i = 0; $i < length($seed); $i++) { # First loop is to generate 1 position differ for (my $bs=1; $bs<=3; $bs++) { my $bval = $bs; if ( substr($seed,$i,1) == $bs) { $bval = 0; } my $ns = $seed; substr($ns,$i,1,$bval); print "$ns\n"; # Second loop for tags in 2 positions differ for (my $j=($i+1); $j < length($seed); $j++) { for (my $cs = 1; $cs<=3;$cs++) { my $cval = $cs; if (substr($ns,$j,1) == $cs) { $cval = $cs; } my $ns2 = $ns; substr($ns2,$j,1,$cval); print "\t$ns2\n"; } } }