#!/usr/bin/perl use strict; use warnings; # This should print: "AABBC" print "\n", DoubleChars("ABC", "AB"); # This should print: "LALA LLAALLAAN N" print "\n", SingleChars("LALA LLLAAALLLLAAAAN NN", "LANG"), "\n\n"; # Okay. Now, make it more complicated... foreach (2..7) { print "\n x $_ : |", DoubleCharsX("ABCD", "ABXY", $_), "|"; } print "\n\n"; foreach (2..7) { print "\n x $_ : |", SingleCharsX("LLLLLLLL-AAAAAAAA", "ABX", $_), "|"; } exit; ############################################ ## FUNCTIONS ## ############################################ # This function doubles every instance of a list of characters in # a string and returns a new string. For example, it can be used # to double every instance of space or new line characters. # # DoubleChars("Helo World!", "!lo ") => "Helloo Woorlld!!" # DoubleChars("Helo World!", "HHH") => "HHelo World!" # # Usage: STRING = DoubleChars(STRING, CHARS) # sub DoubleChars { defined $_[0] && length($_[0]) or return ''; defined $_[1] && length($_[1]) or return $_[0]; my $S = $_[0]; $S =~ s/([\Q$_[1]\E]{1})/$1$1/g; return $S; } ############################################ # # This function removes certain characters that appear twice next # to each and leaves only one instance. This can be used to undo # the effects of the DoubleChars() function. # # SingleChars(" HEELLLLLLO OO!", "OL") => " HEELLLO O!" # SingleChars("AABBCAABBC", "AB") => "ABC" # # Usage: STRING = DoubleChars(STRING, CHARS) # sub SingleChars { defined $_[0] && length($_[0]) or return ''; defined $_[1] && length($_[1]) or return $_[0]; my $S = $_[0]; $S =~ s/([\Q$_[1]\E]{1})\1/$1/g; return $S; } ############################################ # This function doubles or triples every instance of # a list of characters in a string and returns a new string. # For example, it can be used to repeat every instance of # the exclamation point. The second argument tells what # to repeat, and the third argument tells how many times # to repeat those characters. # # DoubleCharsX("Hello World!", "!", 3) => "Hello World!!!" # # Usage: STRING = DoubleCharsX(STRING, CHARS, [REP]) # sub DoubleCharsX { defined $_[0] && length($_[0]) or return ''; defined $_[1] && length($_[1]) or return $_[0]; my $REP = defined $_[2] ? int($_[2]) : 2; $REP >= 2 or $REP = 2; my $S = $_[0]; $S =~ s/([\Q$_[1]\E]{1})/$1 x $REP/ge; return $S; } # This function doubles or triples every instance of # a list of characters in a string and returns a new string. # For example, it can be used to repeat every instance of # the exclamation point. The second argument tells what # to repeat, and the third argument tells how many times # to repeat those characters. # # SingleCharsX("Pressss a keeeeyyy!", "ey", 3) => "Pressss a keey!" # # Usage: STRING = SingleCharsX(STRING, CHARS, [REP]) # sub SingleCharsX { defined $_[0] && length($_[0]) or return ''; defined $_[1] && length($_[1]) or return $_[0]; my $REP = defined $_[2] ? int($_[2]) : 2; $REP >= 2 or $REP = 2; my $S = $_[0]; foreach my $CHAR (split(//, $_[1])) { $S =~ s/\Q$CHAR\E{$REP}/\Q$CHAR\E/g; } return $S; }