use strict; use warnings; my @str = ("j k l foobar", "foobar", "jkl foobar", "1 2 3", "jk l foobar", "foobar j k l", "foobar j kl", " ", " ", "j jk foobar", "j k jk foobar", "j k l"); my @sanitisedNames = (); for(@str) { $_ =~ s/\s+/ /g; if ($_ =~ /^\s$/) { next; } my $boundary = &sanitise($_); my $sanitisedName; if ($boundary == 0) { $sanitisedName = $_; } elsif ($boundary == length($_)) { $_ =~ s/\s+//g; $sanitisedName = $_; } else { my $firstPart = substr($_, 0, $boundary); $firstPart =~ s/\s+//g; my $secondPart = substr($_, $boundary); $sanitisedName = $firstPart.' '.$secondPart; } push(@sanitisedNames, $sanitisedName); } print $_, "\n" for (@sanitisedNames); sub sanitise { my $str = shift; my @chars = split('', $str); my $count = 0; my $len = length($str); while ($count < $len) { if ($chars[$count++] ne ' ' && $count < $len && $chars[$count++] eq ' ') { } else { if ($count == $len) { return $len; } if ($count > 3) { $count = $count - 3; return $count; } else { return 0; } } } }