"Sputnik" help  
Sputnik Help
<expression> =~ tr/searchList/replacementList/flags
<expression> =~ y/searchList/replacementList/flags
tr/searchList/replacementList/flags
y/searchList/replacementList/flags

Description

Translate characters from one character set to another similar to StrTr function but the same as the Tr function.

Parameters

<expression>

Any valid expression that is a string or an array.

You may choose to skip this part and it will use @_ for you.

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
r = Non-destructive it will not modify the original item at all but instead
return a modified copy of it as part of a return array
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.

OR if the "r" flag is used it will return an array where first element of the number of characters replaced or deleted and the second element is a clone of the input with the translation applied.

Remarks

The Sputnik tr/// operator is used to transliterate all occurrences found in a search list with the corresponding character in a replacement list.

This 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.

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 the Sputnik 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.

This provides a powerful and concise way to perform character substitutions in strings. This operator simplifies the task of replacing characters or translating one set of characters to another.

With the tr// y// operators, you can specify a set of characters to be replaced and a corresponding set of characters to replace them with. It operates on a string and performs the substitution in place, modifying the original string.

This feature offers several benefits. First, it enhances code readability by providing a clear and compact syntax for character substitutions. The tr// y// operator eliminates the need for complex regular expressions or multiple function calls to achieve the same result.

Second, the tr// y// operator allows for efficient character substitutions, especially when dealing with large strings or performing multiple replacements. It internally uses optimized algorithms to perform the substitutions quickly and efficiently.

Third, the operator supports both individual character replacements and character ranges. This flexibility enables you to handle a wide range of substitution scenarios, from simple character swaps to complex transformations involving multiple characters.

Overall, the tr// y// operator in Sputnik simplifies the process of character substitutions, offering a concise and efficient syntax. It is a valuable tool for string manipulation, enabling you to perform replacements and translations with ease.

Related

Tr

Example

Simple convert lower case into uppercase:

my $string = "one two three four five six seven";
my $changed = $string =~ tr/a-z/A-Z/;
say $string;
say $changed;
// PRINTS
// ONE TWO THREE FOUR FIVE SIX SEVEN
// 27

Example of using the /r flag to return a modified copy instead of modifying the original:

my $string = "one two three four five six seven";
say "Original string: $string";
my List ($changed, $newString) = $string =~ tr/a-z/A-Z/r;
say "Original string (after tr): $string";
say "New string: $newString";
say "Change count: $changed";
// PRINTS
// Original string: one two three four five six seven
// Original string (after tr): one two three four five six seven
// New string: ONE TWO THREE FOUR FIVE SIX SEVEN
// Change count: 27

Simple convert lower case into uppercase but this time using a variable (possible by the /p flag):

my $string = "one two three four five six seven";
my $s = 'a' rr 'z';
my $changed = $string =~ tr/$s/A-Z/p;
say $string;
say $changed;
// PRINTS
// ONE TWO THREE FOUR FIVE SIX SEVEN
// 27

Removing something you didn't want and replacing with spaces:

my $string = "123.95,44.32,27.77,221q23";
$string =~ tr/,.q/   /; 
say $string;
// 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; 
say $string;
// PRINTS
// 11 222 3 44

Example of using /s to squish:

my $string = "123,,,95,,44,,32,,,,27....77";
$string =~ tr/,./\0/sP;
say $string;
// PRINTS
// 123,95,44,32,27.77
Notice the \0 escape was used in the replacement? This means a blank character ie nothing. Then the P flag causes tells the tr// to parse escapes in the replacement area which causes the \0 to get parsed resulting in an empty string causing all replacements to be an empty string.

Another example of using /s to squish:

my $string = "This . is an 7example 9only";
$string =~ tr/ a-zA-Z.79/\-a-zA-Z/ds;
say $string;
// 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 .....";
my $dots = $string =~ tr/././; 
say "Dots '$dots'";
say "String '$string'";
// 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";
my $changed = $string =~ tr/a-z/0-9/i;
say $string;
say $changed;
// 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;
// PRINTS
// Array
// (
//     [0] => THE
//     [1] => QUICK
//     [2] => BROWN
//     [3] => FOX
// )

If you ignore the $var =~ part and just do the translation part it will use the @_ variable example:

@_ = "cat";
tr/a-z/A-Z/;
say @_;
// PRINTS
// CAT

 


Contact
Cheryl (uberfox@hotmail.com)
Homepage
http://ubersoft.org