abhy has asked for the wisdom of the Perl Monks concerning the following question:
Also can this be done in a single line with a regex? I could not come up with one. So I am coming to the abode of the monks for wisdom :).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; } } } }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: String manipulation
by GrandFather (Saint) on Jun 24, 2009 at 10:59 UTC | |
|
Re: String manipulation
by citromatik (Curate) on Jun 24, 2009 at 10:08 UTC | |
by abhy (Novice) on Jun 24, 2009 at 10:42 UTC | |
by citromatik (Curate) on Jun 24, 2009 at 11:22 UTC | |
by Limbic~Region (Chancellor) on Jun 24, 2009 at 13:03 UTC |