Core Function Translation
(→flags) |
|||
Line 37: | Line 37: | ||
P = Allows the replacementList to be parsed as if it was a "" string so variables etc will be properly parsed and inserted | P = Allows the replacementList to be parsed as if it was a "" string so variables etc will be properly parsed and inserted | ||
+ | |||
+ | Note - p and P work differently in Regexp there they will disable parsing as a string but here p and P enable parsing as a string just something to keep in mind. | ||
+ | |||
+ | Default: None of the flags are enabled by default | ||
=== Return Value === | === Return Value === |
Revision as of 06:30, 16 September 2013
<Expression> =~ tr/searchList/replacementList/flags <Expression> =~ y/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
p = Allows the searchList to be parsed as if it was a "" string so variables etc will be properly parsed and inserted
P = Allows the replacementList to be parsed as if it was a "" string so variables etc will be properly parsed and inserted
Note - p and P work differently in Regexp there they will disable parsing as a string but here p and P enable parsing as a string just something to keep in mind.
Default: None of the flags are enabled by default
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 # this exactly the same y/SEARCHLIST/REPLACEMENTLIST/od # you choose if you want to use tr/ or y/
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
my $string = 'one two three four five six seven'; $changed = $string =~ tr/a-z/A-Z/; say $string; say $changed; # it prints: ONE TWO THREE FOUR FIVE SIX SEVEN # 27
Same as above but this time using a variable (possible by the /p flag)
my $string = 'one two three four five six seven'; my $s = 'a'..'z'; my $changed = $string =~ tr/$s/A-Z/p; say $string; say $changed; # it prints: ONE TWO THREE FOUR FIVE SIX SEVEN
Removing something you didn't want and replacing with spaces
my $string = "123.95,44.32,27.77,221q23"; $string =~ tr/,.q/ /; printr $string; # it prints: 123 95 44 32 27 77 221 23
Converting and removing stuff
my $string = "aa bbb c dd eee ff mmmm rr"; $string =~ tr/a-z/1234/d; printr $string; # it prints: 11 222 3 44
Example of using /s to squish
my $string = "123,,,95,,44,,32,,,,27....77"; $string =~ tr/,.//s; print $string; # it prints: 123,95,44,32,27.77
Another example of using /s to squish
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?
Another example of counting the number of times a character appears in a string
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?
Ignore case
my $string = 'one TWO three four five six seven'; $changed = $string =~ tr/a-z/0-9/i; say $string; say $changed; # it prints: 994 999 97944 5999 5894 989 94949 # 27
Using it on an array
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 #}