#! /usr/bin/perl -w use strict; # build our encoder and decoder my $offset = 0; my %encode; my %decode; for my $x( qw/A C G T/ ) { for my $y( qw/A C G T/ ) { for my $z( qw/A C G T/ ) { $encode{"$x$y$z"} = $offset; $decode{$offset} = "$x$y$z"; $offset++; } } } # encode the string my $in = shift || 'AAAACTGACCGTTTT'; my $enc = ''; while( $in =~ /(...)/g ) { $enc .= chr( 48 + $encode{$1} ); } print "encoded: $enc\n"; # and back again my $dec = ''; for( split //, $enc ) { $dec .= $decode{ ord($_) - 48 }; } print "decoded: $dec\n";