Funky Solution with a Hash
sub delpoint8 ($) { local $_ = shift; my @parts = split /(\.)/; my %hash; for my $i (0..$#parts) { push @{ $hash{$parts[$i]} }, $i; } if (exists $hash{'.'}) { $hash{'.'} = [ @{ $hash{'.'} }[-1] ]; } my %sort; for my $key (%hash) { for my $number (@{$hash{$key}}) { $sort{$number} = $key; } } $_ = ''; for my $number (sort {$a <=> $b} keys %sort) { $_ .= $sort{$number}; } return $_; }