use List::Util qw[ max ]; sub encoding { my @characters = ( 'A' .. 'Z', 'a' .. 'z', 0 .. 9, '-', '.' ); my $max = max( $_[ 0 ] =~ /\d+/g ); return join ',', map join( '', map $characters[ $_ / 64 ] . $characters[ $_ % 64 ], map $_ / $max * 4095, split /,/ ), split /\|/, $_[ 0 ]; }