Core Function Translation
(Created page with "<pre> <Expression> =~ s/searchList/replacementList/flags </pre> === Description === Translate characters from one character set to another similar to StrTr() function. === Par...")
Newer edit →
Revision as of 23:27, 15 September 2013
<Expression> =~ s/searchList/replacementList/flags
Contents |
Description
Translate characters from one character set to another similar to StrTr() function.
Parameters
searchList
A pattern to use to create the searchList see Remarks below.
This list will become a sequence of characters to be replaced.
replacementList
A pattern to use to create the replacementList see Remarks below.
This list will become a sequence of characters to be replace the corresponding character (at the same index number) in the searchList.
flags
Optional; The flags to use in the operation.
i = Ignore case.
d - Delete found but not replaced characters
o = Do not recompile the character sets after the first compile (Improves speed of your function if you run the operation many times).
s = Makes it so the sequences of characters that were transliterated to the same character are squashed down to a single instance of the character
Return Value
Returns number of characters replaced or deleted.
Remarks
The Sputnik tr/// function is used to transliterate all occurrences found in a search list with the corresponding character in a replacement list.
This function is a character by character translation and if you want to perform more complex operations you can use the <Expression> =~ s/pattern/replacement/flags regex substitution operator instead.
In Sputnik tr/// is known better as an operator rather than a function.
The syntax form of the tr/// operator is as follows:
tr/SEARCHLIST/REPLACEMENTLIST/od <pre> It replaces all the occurrences of the characters in SEARCHLIST with the characters in the REPLACEMENTLIST. It returns the number of characters replaced or deleted. The transliteration table is built at run time, so you can use interpolation either in SEARCHLIST or in REPLACEMENTLIST including variables as long as you set the appropriate flag for that. The o, d are modifiers that add additional functionality to the Sputnik tr/// operator (see Flags above). The strings can be specified via the =~. Please note that Sputnik's tr/// operator does not do regular expressions character classes such as \d. One character that has a special meaning to tr/// is the dash (-) and it indicates a range of characters (for example tr/A-C/a-c/). The slash (/) has the meaning of a delimiter, because it separates the arguments of the tr/// operator. tr// like all Sputnik functions fully supports Unicode. === Example === Simple convert lower case into uppercase <syntaxhighlight lang="sputnik"> my $string = 'one two three four five six seven'; $string =~ tr/a-z/A-Z/; say $string; # it prints: ONE TWO THREE FOUR FIVE SIX SEVEN </syntaxhighlight> Removing something you didn't want and replacing with spaces <syntaxhighlight lang="sputnik"> my $string = "123.95,44.32,27.77,221q23"; $string =~ tr/,.q/ /; printr $string; # it prints: 123 95 44 32 27 77 221 23 </syntaxhighlight> Converting and removing stuff <syntaxhighlight lang="sputnik"> my $string = "aa bbb c dd eee ff mmmm rr"; $string =~ tr/a-z/1234/d; printr $string; # it prints: 11 222 3 44 </syntaxhighlight> Example of using /s to squish <syntaxhighlight lang="sputnik"> my $string = "123,,,95,,44,,32,,,,27....77"; $string =~ tr/,.//s; print $string; # it prints: 11 222 3 44 </syntaxhighlight> Another example of using /s to squish <syntaxhighlight lang="sputnik"> my $string = "This . is an 7example 9only"; $string =~ tr/ a-zA-Z.79/\-a-zA-Z/ds; printr $string; # it prints: This-is-an-example-only # Notice it will remember what it did previously and still squish? </syntaxhighlight> Another example of counting the number of times a character appears in a string <syntaxhighlight lang="sputnik"> my $string = "This pencil is .... old. You can ..... it ....."; $dots = $string =~ tr/././; say "Dots '$dots' String '$string'"; # it prints: Dots '15' String 'This pencil is .... old. You can ..... it .....' # notice the string remains unmodified? </syntaxhighlight> Using it on an array <syntaxhighlight lang="sputnik"> my $string = array("The", "quick", "brown", "fox"); $string =~ tr/a-z/A-Z/; printr $string; # it prints: #ARRAY #{ # [0] => THE # [1] => QUICK # [2] => BROWN # [3] => FOX #} </syntaxhighlight> [[Category:Core Function]]