S
steve_f
I want to transform special German characters to obtain the following
variations:
groß bräu
gross bräu
gross braeu
there are two sets -
set one:
ß = ss = \xDF
set two:
Ä = Ae = \xC4
Ö = Oe = \xD6
Ü = Ue = \xDC
ä = ae = \xE4
ö = oe = \xF6
ü = ue = \xFC
basically, the rules are transform ß independently
and with set two, they are either all on or off together.
I wrote the follow which works well, but looks
pretty bad I think. so again this is a style question...
can anyone suggest a cleaner approach? TIA
sub transform_characters {
my @input = @_;
my @output;
for my $string (@input) {
push @output, $string;
if ($string =~ /\xDF/) {
$string =~ s/\xDF/ss/g;
push @output, $string;
if (test_for_character($string)) {
$string = swap_all($string);
push @output, $string;
}
next;
}
if (test_for_character($string)) {
$string = swap_all($string);
push @output, $string;
}
}
return @output;
}
sub test_for_character {
my $string = shift;
if ($string =~ /\xC4/ ||
$string =~ /\xD6/ ||
$string =~ /\xDC/ ||
$string =~ /\xE4/ ||
$string =~ /\xF6/ ||
$string =~ /\xFC/) {
return 1
} else {
return 0
}
}
sub swap_all {
my $string = shift;
$string =~ s/\xC4/Ae/g;
$string =~ s/\xD6/Oe/g;
$string =~ s/\xDC/Ue/g;
$string =~ s/\xE4/ae/g;
$string =~ s/\xF6/oe/g;
$string =~ s/\xFC/ue/g;
return $string;
}
variations:
groß bräu
gross bräu
gross braeu
there are two sets -
set one:
ß = ss = \xDF
set two:
Ä = Ae = \xC4
Ö = Oe = \xD6
Ü = Ue = \xDC
ä = ae = \xE4
ö = oe = \xF6
ü = ue = \xFC
basically, the rules are transform ß independently
and with set two, they are either all on or off together.
I wrote the follow which works well, but looks
pretty bad I think. so again this is a style question...
can anyone suggest a cleaner approach? TIA
sub transform_characters {
my @input = @_;
my @output;
for my $string (@input) {
push @output, $string;
if ($string =~ /\xDF/) {
$string =~ s/\xDF/ss/g;
push @output, $string;
if (test_for_character($string)) {
$string = swap_all($string);
push @output, $string;
}
next;
}
if (test_for_character($string)) {
$string = swap_all($string);
push @output, $string;
}
}
return @output;
}
sub test_for_character {
my $string = shift;
if ($string =~ /\xC4/ ||
$string =~ /\xD6/ ||
$string =~ /\xDC/ ||
$string =~ /\xE4/ ||
$string =~ /\xF6/ ||
$string =~ /\xFC/) {
return 1
} else {
return 0
}
}
sub swap_all {
my $string = shift;
$string =~ s/\xC4/Ae/g;
$string =~ s/\xD6/Oe/g;
$string =~ s/\xDC/Ue/g;
$string =~ s/\xE4/ae/g;
$string =~ s/\xF6/oe/g;
$string =~ s/\xFC/ue/g;
return $string;
}