TATMWTDI There are too many ways to do it #18

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 $_;
}
Copyright © 2005 Shlomi Fish